CreateDirStruct <- function(WkgDir=getwd(),ConfigDirectory="Config",PLs=PipeLineLocations){

  dir.create(PLs@BamDir,recursive=F,showWarnings=F)
  dir.create(PLs@FQDir,recursive=F,showWarnings=F)
  dir.create(PLs@FragLengthDir,recursive=F,showWarnings=F)
  dir.create(PLs@LocationsDir,recursive=F,showWarnings=F)
  dir.create(PLs@TempDir,recursive=F,showWarnings=F)
  dir.create(PLs@WorkFlowDir,recursive=F,showWarnings=F)
  
  system(paste("mkdir -p ",PLs@MacsDir,sep=""),wait=T)  
  system(paste("mkdir -p ",PLs@SicerDir,sep=""),wait=T)  
  system(paste("mkdir -p ",PLs@TPICsDir,sep=""),wait=T)  
      
  system(paste("lfs setstripe ",PLs@BamDir,sep=""),wait=T)
  system(paste("lfs setstripe ",PLs@FQDir,sep=""),wait=T)
  system(paste("lfs setstripe ",PLs@MacsDir,sep=""),wait=T)  
  system(paste("lfs setstripe ",PLs@SicerDir,sep=""),wait=T) 
  system(paste("lfs setstripe ",PLs@TPICsDir,sep=""),wait=T)     
  
}

UnlockSampleSheet <- function(WkgDir){
  if(file.exists(file.path(WkgDir,"SampleSheet.LOCK"))){
    unlink(file.path(WkgDir,"SampleSheet.LOCK"))
  }

}

newXMLNode <- function (name, ..., attrs = NULL, namespace = character(), namespaceDefinitions = character(),
    doc = NULL, .children = list(...), parent = NULL, at = NA,
    cdata = FALSE, suppressNamespaceWarning = getOption("suppressXMLNamespaceWarning",
        FALSE), sibling = NULL, addFinalizer = NA, noNamespace = length(namespace) ==
        0 && !missing(namespace))
{
    if (length(attrs)) {
        ids = names(attrs)
        attrs = structure(as(attrs, "character"), names = ids)
        i = grep("^xmlns", names(attrs))
        if (length(i)) {
            warning("Don't specify namespace definitions via 'attrs'; use namespaceDefinitions")
            namespace = c(namespace, structure(attrs[i], names = gsub("^xmlns:",
                "", names(attrs)[i])))
            attrs = attrs[-i]
        }
    }
    else attrs = character()
    ns = character()
    name = strsplit(name, ";")[[1]]
    if (length(name) == 2) {
        ns = name[1]
        name = name[2]
        noNamespace = FALSE
    }
    if (is.list(parent)) {
        if (length(parent) < 1 || !(is(parent[[1]], "XMLInternalElementNode") ||
            is(parent[[1]], "XMLInternalDocument")))
            stop("incorrect value for parent")
        parent = parent[[1]]
    }
    if (missing(doc) && !missing(parent) && inherits(parent,
        "XMLInternalDocument")) {
        doc = parent
        parent = NULL
    }
    if (is.null(doc) && !is.null(parent)) {
        doc = if (inherits(parent, "XMLInternalDocument"))
            parent
        else .Call("R_getXMLNodeDocument", parent, PACKAGE = "XML")
    }
    node <- .Call("R_newXMLNode", as.character(name), character(),
        character(), doc, namespaceDefinitions, addFinalizer,
        PACKAGE = "XML")
    if (!is.null(sibling))
        addSibling(sibling, node, after = as.logical(at))
    else if (!is.null(parent))
        addChildren(parent, node, at = at)
    if (TRUE) {
        nsDefs = lapply(seq(along = namespaceDefinitions), function(i) newNamespace(node,
            namespaceDefinitions[[i]], names(namespaceDefinitions)[i],
            set = FALSE))
        if (length(namespaceDefinitions))
            names(nsDefs) = if (length(names(namespaceDefinitions)))
                names(namespaceDefinitions)
            else ""
    }
    else nsDefs = xmlNamespaceDefinitions(node)
    addAttributes(node, .attrs = attrs, suppressNamespaceWarning = suppressNamespaceWarning)
    if (is(namespace, "XMLNamespaceRef")) {
        setInternalNamespace(node, namespace)
    }
    else if (is.na(noNamespace) || !noNamespace) {
        ns =  XML:::getNodeNamespace(ns, nsDefs, node, namespace, noNamespace,
            namespaceDefinitions, parent, suppressNamespaceWarning)
        if (is.null(ns))
            .Call("R_setNamespaceFromAncestors", node, PACKAGE = "XML")
    }
    if (length(ns) && (inherits(ns, c("XMLNamespaceRef", "XMLNamespaceDeclaration")) ||
        (is.character(ns) && ns != "")))
        setXMLNamespace(node, ns)
    if (length(.children)) {
        if (!is.list(.children))
            .children = list(.children)
        addChildren(node, kids = .children, cdata = cdata, addFinalizer = addFinalizer)
    }
    node
}


ReadAndLock <- function(ss,WkdDir,SAF=T,napTime=5){
  if(file.exists(gsub(".csv",".LOCK",ss))){
    while(file.exists(gsub(".csv",".LOCK",ss))){
      Sys.sleep(napTime)
    }
    write.table("Locked",gsub(".csv",".LOCK",ss))
    SampleSheet <- read.delim(ss,stringsAsFactors=SAF,sep=",")
  }else{
    write.table("Lcoked",gsub(".csv",".LOCK",ss))
    SampleSheet <- read.delim(ss,stringsAsFactors=SAF,sep=",")
  }
  return(SampleSheet)

}

WriteAndUnlock <- function(SampleSheet,ss){
   if(file.exists(gsub(".csv",".LOCK",ss))){
     write.table(SampleSheet,ss,sep=",",row.names=F,quote=F)
     unlink(gsub(".csv",".LOCK",ss))
   }else{
     write.table(SampleSheet,ss,sep=",",row.names=F,quote=F)
   }
}

GetUsernames <- function(){
  UserNameList <- vector("list",length=2)
  UserName = system("id -nu",wait=TRUE,intern=TRUE)
  MegaName <- paste("cri.camres.org\\\\",UserName,"@uk-cri-larc01",sep="")
  UserNameList[[1]] <-  UserName
  UserNameList[[2]] <-  MegaName
  return(UserNameList)
}

getRandString<-function(len=12){
  return(paste(sample(c(rep(0:9,each=5),LETTERS,letters),len,replace=TRUE),collapse=''))
}


GetImportantLocations <- function(WkgDir=getwd(),ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  TempDirTemp <- ConfigFile[ConfigFile[,2] %in% "tempdirectory",3]
  WkgDirTemp <- ConfigFile[ConfigFile[,2] %in% "workingdirectory",3]
  LocationsDirTemp <- ConfigFile[ConfigFile[,2] %in% "locationsdirectory",3]
  BamDirTemp <- ConfigFile[ConfigFile[,2] %in% "bamdirectory",3]
  FQDirTemp <- ConfigFile[ConfigFile[,2] %in% "fastqdirectory",3]
  WorkFlowDirTemp <- ConfigFile[ConfigFile[,2] %in% "workflowdirectory",3]
  FragLengthDirTemp <- ConfigFile[ConfigFile[,2] %in% "fraglengthdirectory",3] 
  MacsDirTemp <- ConfigFile[ConfigFile[,2] %in% "macsdirectory",3]   
  SicerDirTemp <- ConfigFile[ConfigFile[,2] %in% "sicerdirectory",3]  
  TPICsDirTemp <- ConfigFile[ConfigFile[,2] %in% "tpicsdirectory",3]  
  setClass("ChIPDirLocations", representation(TempDir = "character",WkgDir = "character",LocationsDir = "character",BamDir = "character",FQDir = "character",WorkFlowDir="character",FragLengthDir="character",MacsDir="character",SicerDir="character",TPICsDir="character"))
  PLLocations <- new("ChIPDirLocations",TempDir=TempDirTemp,WkgDir=WkgDirTemp,LocationsDir=LocationsDirTemp,BamDir=BamDirTemp,FQDir=FQDirTemp,WorkFlowDir=WorkFlowDirTemp,FragLengthDir=FragLengthDirTemp,MacsDir=MacsDirTemp,SicerDir=SicerDirTemp,TPICsDir=TPICsDirTemp)
  return(PLLocations)
}

GetSampleSheetOrDummy <- function(WkgDir=getwd(),SampleSheet="SampleSheet.csv"){
  if(file.exists(file.path(WkgDir,SampleSheet))){
    SampleSheet <- read.delim("SampleSheet.csv",sep=",",header=T,stringsAsFactors=F)
    if(length(grep("^bamFileName$",colnames(SampleSheet))) > 0){
        colnames(SampleSheet)[grep("^bamFileName$",colnames(SampleSheet))] <- "Source_File"
    }
    if(!length(grep("^BamLocation$",colnames(SampleSheet))) > 0){
              colnames(SampleSheet)[grep("^Location$",colnames(SampleSheet))] <- "BamLocation"
    }
    if(!length(grep("^FQLocation$",colnames(SampleSheet))) > 0){
              SampleSheet[,"FQLocation"] <- NA
    }
    if(!length(grep("^BarcodesFile$",colnames(SampleSheet))) > 0){
              SampleSheet[,"BarcodesFile"] <- NA
    }
    if(!length(grep("^Analysis_State$",colnames(SampleSheet))) > 0){
              SampleSheet[,"Analysis_State"] <- "Local_SetToRun"
    }


  }else{
    SampleSheet <- matrix(nrow=1,ncol=30)
    colnames(SampleSheet) <- c("GenomicsID","SampleName","Analysis_State","Run","Lane","BamLocation","FQLocation","BarcodesFile","Tissue","Factor","Antibody","Condition_1","Condition_2","Replicate","InputToUse","toMerge","Source_File","Processed_bamFileName","Original","delRand","Excluded","Filtered","Unique","DuplicationRate","InputFileNameToUse","derivedFrom","Macs_Fragment_Length","Sissr_Fragment_Length","Correlation_Fragment_Length","Coverage_Fragment_Length")
  }
  return(SampleSheet)
}

GetLocal <- function(SS=SampleSheet,PLs=PipeLineLocations){
#  SamplesWeHave <- NULL
#  SamplesFoundFromPrevious <- vector("character")
#  SamplesNotFoundPrevious <- vector("character")
#  SamplesInLocations <- SS[,"Source_File"]
  AsBams <-  dir(file.path(PLs@BamDir),pattern="*.bam")
  FQDirs <-   list.dirs(file.path(PLs@FQDir),recursive=F)
  AllFQs <- dir(FQDirs,full.names=T)
  for(i in 1:nrow(SS)){
    if(SS[i,"Source_File"] %in% AsBams | SS[i,"Source_File"] %in% AllFQs){
      print("Found one!")
    }else{
       SS[i,"Source_File"] <- "Local_Sample_Not_Found"
    }
    
  }
  
  if(any(SS[,"Source_File"] %in% "Local_Sample_Not_Found")){
    write.table(SS[SS[,"Source_File"] %in% "Local_Sample_Not_Found","SampleName"],file.path(PLs@LocationsDir,"Missing_Local_Samples.txt"),sep="\t",quote=F,row.names=F)
#    system(paste("cat",file.path(PLs@LocationsDir,"Missing_Local_Samples.txt"),file.path(PLs@LocationsDir,"SamplenamesFromLims.txt"),sep=" "),wait=T)
    if(file.info(file.path(PLs@LocationsDir,"SamplenamesFromLims.txt"))$size > 0){
      SamplesInLims <- as.vector(read.delim(file.path(PLs@LocationsDir,"SamplenamesFromLims.txt"),header=F)[,1])
    }else{
      SamplesInLims <- ""
    }
    SamplesInLims <- as.vector(na.omit(unique(c(SS[SS[,"Source_File"] %in% "Local_Sample_Not_Found","SampleName"],SamplesInLims))))
    write.table(SamplesInLims,file.path(PLs@LocationsDir,"SamplenamesFromLims.txt"),quote=F,row.names=F,col.names=F)
  }
  return(SS)     
}

#PythonScriptForLims=Scripts@LimsCall

GetSamplesFromLims <- function(SampleSheet=SampleSheet,PLs=PipeLineLocations,ProjectsFile="ProjectsFromLims.txt",SLXIDFile="SLXIDsFromLims.txt",SamplenamesFile="SamplenamesFromLims.txt",PythonForLims="/home/mib-cri/bin/python",PythonScriptForLims="/lustre/mib-cri/carrol09/Work/MyPipe/Process10/PythonScripts/AnotherProcess.py"){
  CallLimsCommand <- paste(PythonForLims,PythonScriptForLims,PipeLineLocations@LocationsDir,ProjectsFile,SLXIDFile,SamplenamesFile,sep=" ")
  LimsLog <- system(CallLimsCommand,wait=T,intern=T)  
  ResFromLimsCall <- paste("From Lims", paste(LimsLog[grep("Found",LimsLog)],collapse="\n"),sep="\n")
  return(ResFromLimsCall)
}

GetBarcodes <- function(PLs=PipeLineLocations){
    BarcodesDirectory <- file.path(PipeLineLocations@LocationsDir,"barcodes")
    BarcodesFiles <-  dir(BarcodesDirectory,full.names=T)
    BarcodesFileNames <-  dir(BarcodesDirectory,full.names=F)
    Barcodes <- vector("list",length=length(BarcodesFileNames))
    if(length(Barcodes) != 0){
      for(i in 1:length(BarcodesFiles)){
       TempCodes <- read.delim(BarcodesFiles[i],sep="\t",header=F)
       Barcodes[[i]] <- TempCodes
      }
    }
    names(Barcodes) <- gsub(".txt","",BarcodesFileNames)
    return(Barcodes)
}

CheckFromDemultiplex <- function(PLs=PipeLineLocations,SS=SampleSheet){
  Barcodes <- GetBarcodes(PLs)
  TempBarcodes <- matrix(nrow=1,ncol=ncol(SampleSheet))
  colnames(TempBarcodes) <- colnames(SS)
  NotKnownSS <- SS[(SS[,"FQLocation"]) %in% "Location_Not_Known",]
  SS <-  SS[!(SS[,"FQLocation"]) %in% "Location_Not_Known",]
  for(i in 1:nrow(SS)){
      ID <- SS[i,"GenomicsID"]
      if(length(grep("^SLX",ID)) > 0){
            ID <- gsub("\\..*","",ID)
      }
      if(any(names(Barcodes) %in% ID)){
          SS[i,"Analysis_State"] <- "Parent_Of_Sample"
          BarcodesForSamples <- Barcodes[names(Barcodes) %in% ID]          
          for(k in 1:nrow(BarcodesForSamples[[1]])){
            EvenTemperRow <- vector(length=ncol(SampleSheet)) 
            names(EvenTemperRow) <- colnames(SS)
            BarcodeInfo <- as.vector(BarcodesForSamples[[1]][k,])
            EvenTemperRow["GenomicsID"] <- paste(SS[i,"GenomicsID"],BarcodeInfo[,2],sep="")
            EvenTemperRow["Analysis_State"] <- "RunMe"            
            EvenTemperRow["Source_File"] <- SS[i,"GenomicsID"]
            EvenTemperRow["SampleName"] <- as.character(as.vector(BarcodeInfo[,2]))
            EvenTemperRow[c("Run","Lane")] <- SS[i,c("Run","Lane")]
            EvenTemperRow[c("FQLocation","BamLocation")] <- "Premultiplex"
            EvenTemperRow[EvenTemperRow == "FALSE"] <- NA            
            TempBarcodes <- rbind(TempBarcodes,EvenTemperRow)
          }
          SS[i,"BarcodesFiles"] <- dir(file.path(PLs@LocationsDir,"barcodes"),full.names=T)[dir(file.path(PLs@LocationsDir,"barcodes")) %in% paste(names(BarcodesForSamples),".txt",sep="")]
      }
  }
  SS <- rbind(SS,NotKnownSS,TempBarcodes)
  return(SS)
}


WriteLog <- function(WhatsLocal=WhatsLocal,LimsLog=LimsLog,ToDemultiplex=ToDemultiplex,PLs=PipeLineLocations){
  cat(WhatsLocal,LimsLog,"\n\n",ToDemultiplex,"\n",file=file.path(PLs@LocationsDir,"SamplesLog.txt"),append = TRUE)
}

GetWorkFlowConfig <- function(WkgDir=getwd(),ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  
  MetaVersion <- ConfigFile[ConfigFile[,2] %in% "metaversion",3]
  XSI <- ConfigFile[ConfigFile[,2] %in% "xsi",3]
  SchemaLocation <- ConfigFile[ConfigFile[,2] %in% "schemalocation",3]
  Mode <- ConfigFile[ConfigFile[,2] %in% "mode",3]
  Pipeline <- ConfigFile[ConfigFile[,2] %in% "pipeline",3]
  TaskDirectories <- ConfigFile[ConfigFile[,2] %in% "taskdirectories",3]
  TempDirectory <- ConfigFile[ConfigFile[,2] %in% "tempdirectory",3]
  SummaryFile <- ConfigFile[ConfigFile[,2] %in% "summaryfile",3]
  SummaryErrors <- ConfigFile[ConfigFile[,2] %in% "summaryerrors",3]
  Queue <- ConfigFile[ConfigFile[,2] %in% "queue",3]
  setClass("WorkFlowConfig", representation(MetaVersion = "character",XSI = "character",SchemaLocation = "character",Mode= "character",Pipeline= "character",
  TaskDirectories = "character",TempDirectory = "character",SummaryFile = "character",SummaryErrors = "character",
  Queue = "character"
  ))
  PLWorkFlow <- new("WorkFlowConfig",MetaVersion=MetaVersion,XSI=XSI,SchemaLocation=SchemaLocation,Mode=Mode,Pipeline=Pipeline,
  TaskDirectories = TaskDirectories,TempDirectory = TempDirectory,SummaryFile = SummaryFile,SummaryErrors = SummaryErrors,
  Queue = Queue  
  )
  return(PLWorkFlow)
}

MakeWorkFlowXML<- function(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75){
  require(XML)      
  WorkFlowConfig <- GetWorkFlowConfig(WkgDir=WkgDir,ConfigDirectory=Config)
  PLs <- GetImportantLocations(WkgDir,"Config")
  MetaVersion=WorkFlowConfig@MetaVersion                                           
  XSI=WorkFlowConfig@XSI
  SchemaLocation=WorkFlowConfig@SchemaLocation
  Mode = WorkFlowConfig@Mode
  TaskDirectories = unlist(strsplit(WorkFlowConfig@TaskDirectories,";"))
  TempDirectory =    WorkFlowConfig@TempDirectory
  SummaryErrors = WorkFlowConfig@SummaryErrors
  #lsfOutputDirectory = WorkFlowConfig@lfsOutputDirectory
  queue = WorkFlowConfig@Queue

  Pipeline = Pipeline
  maxJobs = maxJobs
  maxCpuResources = 1
  MainVariables <- Variables
  names(MainVariables) <- names(Variables)

  dir.create(file.path(PLs@WorkFlowDir,paste(PipeName,JobString,sep="")),showWarnings = FALSE)
  SummaryFile   =    file.path(PLs@WorkFlowDir,paste(PipeName,JobString,sep=""),"SummaryFile.txt")
  lsfOutputDirectory = file.path(PLs@WorkFlowDir,paste(PipeName,JobString,sep=""))

MetaXML <- newXMLDoc()
namespaceVariable <- c(MetaVersion,XSI)
names(namespaceVariable) <- c("meta","xsi")
attrs <- SchemaLocation
names(attrs) <- "xsi:schemaLocation"
GlobalNode <- newXMLNode("meta:metadata",namespace=namespaceVariable,attrs=attrs,suppressNamespaceWarning=T,parent = MetaXML)
  PipelineNode <- newXMLNode("pipeline",Pipeline)
  tempDirectoryNode <- newXMLNode("tempDirectory",TempDirectory)
  errorsonly <- SummaryErrors
  names(errorsonly) <- "errorsOnly"
  SummaryFileNode <- newXMLNode("summaryFile",attrs=errorsonly,SummaryFile)
  ModeNode <- newXMLNode("mode",Mode)
  TaskNode <- newXMLNode("taskDirectories")
  for(i in 1:length(TaskDirectories)){
    newXMLNode("directory",TaskDirectories[i],parent=TaskNode)
  }
  ExecConfigNode <- newXMLNode("executionConfiguration")
  modeattrs <- "lsf"
  names(modeattrs) <- "mode"
    executionNode1 <- newXMLNode("execution",attrs=modeattrs,parent=ExecConfigNode)
      LSFOutputNode <- newXMLNode("lsfOutputDirectory",lsfOutputDirectory,parent=executionNode1)
      queueNode <- newXMLNode("queue",queue,parent=executionNode1)
      MaxJobsNode <- newXMLNode("maximumSubmittedJobs",Pipeline,parent=executionNode1)
      localattrs <- "local"
      names(localattrs) <- "mode"
    executionNode2 <- newXMLNode("execution",attrs=localattrs,parent=ExecConfigNode)
      MaxJobsNode <- newXMLNode("maxCpuResources",Pipeline,parent=executionNode2)
   MainVariableNode <- newXMLNode("variables")
   for(i in 1:length(MainVariables)){
    newXMLNode(names(MainVariables)[i],MainVariables[i],parent=MainVariableNode)
   }
   if(length(Specialisations) > 0){
   MainSpecialNode <- newXMLNode("specialisations")
   SpecialisationNodeList <- vector("list",length=length(Specialisations))
   for(i in 1:length(Specialisations)){
   identifierattrs <- c(names(Specialisations)[i],"true")
   names(identifierattrs) <- c("identifier","active")
      SpecialisationNodeList[[i]] <- newXMLNode("specialisation",attrs=identifierattrs)
      VectorNodeList <- vector("list",length=length(SpecialisationNodeList[[i]]))  
      TempVariableNode <- newXMLNode("variables",parent=SpecialisationNodeList[[i]])
         for(k in 1:length(Specialisations[[i]])){
            newXMLNode(names(Specialisations[[i]])[k],Specialisations[[i]][k],parent=TempVariableNode)
          }
   }
   MainSpecialNode <- addChildren(MainSpecialNode,kids=SpecialisationNodeList,cdata=F)
   
  MetafileMainNode <-  list(PipelineNode,tempDirectoryNode,SummaryFileNode,ModeNode,TaskNode,ExecConfigNode,MainVariableNode,MainSpecialNode)
  }else{
   MetafileMainNode <-  list(PipelineNode,tempDirectoryNode,SummaryFileNode,ModeNode,TaskNode,ExecConfigNode,MainVariableNode)  
  }
  GlobalNode2 <- addChildren(GlobalNode,kids=MetafileMainNode,cdata=F)

  return(MetaXML)
}

GetGenomeFromConfig <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  return(ConfigFile[ConfigFile[,"section"] == "Analysis Settings" & ConfigFile[,"name"] == "genome","value"])  
}

GetGenomeBuildFromConfig <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  genome <- ConfigFile[ConfigFile[,"section"] == "Analysis Settings" & ConfigFile[,"name"] == "genome","value"]
  genomeBuild <- ConfigFile[ConfigFile[,"section"] == "Genomes" & tolower(ConfigFile[,"name"]) == tolower(genome),"value"]
  return(genomeBuild)
}

GetChrLengthsFromConfig <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  genome <- ConfigFile[ConfigFile[,"section"] == "Analysis Settings" & ConfigFile[,"name"] == "genome","value"]
  ChrLengths <- ConfigFile[ConfigFile[,"section"] == "Chromosome Lengths" & tolower(ConfigFile[,"name"]) == tolower(genome),"value"]
  return(ChrLengths)
}

GetSequenceDictionaryFromConfig <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  genome <- ConfigFile[ConfigFile[,"section"] == "Analysis Settings" & ConfigFile[,"name"] == "genome","value"]
  SeqDict <- ConfigFile[ConfigFile[,"section"] == "Sequence Dictionary" & tolower(ConfigFile[,"name"]) == tolower(genome),"value"]
  return(SeqDict)
}

GetExcludedListFromConfig <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  genome <- ConfigFile[ConfigFile[,"section"] == "Analysis Settings" & ConfigFile[,"name"] == "genome","value"]
  ExclRegions <- ConfigFile[ConfigFile[,"section"] == "Excluded Regions" & tolower(ConfigFile[,"name"]) == tolower(genome),"value"]
  return(ExclRegions)
}

getMacsGenome <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  genome <- ConfigFile[ConfigFile[,"section"] == "Analysis Settings" & ConfigFile[,"name"] == "genome","value"]
  genomeformacs <- ConfigFile[ConfigFile[,"section"] == "Macs Parameters" & tolower(ConfigFile[,"name"]) == tolower(genome),"value"]
  return(genomeformacs)
}

getMacsmfold <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  mfold <- ConfigFile[ConfigFile[,"section"] == "Macs Parameters" & ConfigFile[,"name"] == "mfold","value"]
  return(mfold)
}

getMacsShiftSize <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  ShiftSize <- ConfigFile[ConfigFile[,"section"] == "Macs Parameters" & ConfigFile[,"name"] == "shiftsizedefault","value"]
  return(ShiftSize)
}

getSicerWindowSize <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  WindowSize <- ConfigFile[ConfigFile[,"section"] == "Sicer Parameters" & ConfigFile[,"name"] == "window","value"]
  return(WindowSize)
}

getSicerGapSize <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  GapSize <- ConfigFile[ConfigFile[,"section"] == "Sicer Parameters" & ConfigFile[,"name"] == "gapsize","value"]
  return(GapSize)
}

getSicerExec  <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  exec <- ConfigFile[ConfigFile[,"section"] == "Sicer Parameters" & ConfigFile[,"name"] == "sicerexecutable","value"]
  return(exec)
}

getTPICSminSize <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  minsize <- ConfigFile[ConfigFile[,"section"] == "TPICs Parameters" & ConfigFile[,"name"] == "minsize","value"]
  return(minsize)
}

getTPICSwideSize  <- function(WkgDir,ConfigDirectory="Config"){
  require(raster)
  ConfigToRead = file.path(WkgDir,ConfigDirectory,"config.ini")
  ConfigFile <- readIniFile(ConfigToRead)
  wideSize <- ConfigFile[ConfigFile[,"section"] == "TPICs Parameters" & ConfigFile[,"name"] == "widesizeregion","value"]
  return(wideSize)
}



RefreshSampleSheet <- function(SS=SampleSheet,PLs=PipeLineLocations){
  SS_Samples <- unique(as.character(SS[,1]))
#  SS_Samples_SLXIDS <-  gsub("\\..*","",SS_Samples[grep("^SLX",SS_Samples)])
 # names(SS_Samples_SLXIDS) <- SS_Samples[grep("^SLX",SS_Samples)]
  #SS_Samples_NoSLXIDS <-  SS_Samples[!grep("^SLX",SS_Samples)]
 # names(SS_Samples_NoSLXIDS) <- SS_Samples[!grep("^SLX",SS_Samples)]

#  LocalSamples <- read.delim()
  LimsSamples <- read.delim(file.path(PLs@LocationsDir,"Lims_SampleLocations.txt"),sep="\t",header=F)

  NewLimsFrame <- cbind(paste(LimsSamples[,1],".",LimsSamples[,4],".s_",LimsSamples[,3],sep=""),as.vector(LimsSamples[,1]),as.vector(LimsSamples[,2]),paste("Run-",LimsSamples[,4],sep=""),paste("Lane-",LimsSamples[,3],sep=""),file.path(LimsSamples[,5],LimsSamples[,6]),file.path(LimsSamples[,7],LimsSamples[,8]),as.vector(LimsSamples[,9]))
  UpdateLimsFrame <- NewLimsFrame[NewLimsFrame[,1] %in% SS_Samples,]
  NewLimsFrame <- NewLimsFrame[!NewLimsFrame[,1] %in% SS_Samples,]
  NewLimsFrame <- NewLimsFrame[match(unique(NewLimsFrame[,1]),NewLimsFrame[,1]),]
  
  ##Make NewSS
  GenomicsID <- c(SS[match(SS[,1],SS_Samples),"GenomicsID"],as.vector(NewLimsFrame[,1]))
  SampleName <- c(SS[match(SS[,1],SS_Samples),"SampleName"],as.vector(NewLimsFrame[,3]))
  Run <- c(SS[match(SS[,1],SS_Samples),"Run"],as.vector(NewLimsFrame[,4]))
  Lane <- c(SS[match(SS[,1],SS_Samples),"Lane"],as.vector(NewLimsFrame[,5]))
  BamLocation <-  c(SS[match(SS[,1],SS_Samples),"BamLocation"],as.vector(NewLimsFrame[,6]))
  FQLocation <-  c(SS[match(SS[,1],SS_Samples),"FQLocation"],as.vector(NewLimsFrame[,7]))
  Analysis_State <- c(SS[match(SS[,1],SS_Samples),"Analysis_State"],as.vector(NewLimsFrame[,8]))
   
  BarcodesFilesTemp <- c(SS[match(SS[,1],SS_Samples),"BarcodesFile"])
  BarcodesFiles <- c(BarcodesFilesTemp,rep(NA,length(BamLocation)-length(BarcodesFilesTemp)))
#  Analysis_State <- c(Analysis_StateTemp,rep(NA,length(BamLocation)-length(Analysis_StateTemp))) 
  if(nrow(SS) > 1){   
    TempMetaData <- as.matrix(SS[,colnames(SS) %in% c("Tissue","Factor","Antibody","Condition_1","Condition_2","Replicate","InputToUse","toMerge")],ncol=8)
  }else{
    TempMetaData <- matrix(SS[,colnames(SS) %in% c("Tissue","Factor","Antibody","Condition_1","Condition_2","Replicate","InputToUse","toMerge")],ncol=8)
  }
  colnames(TempMetaData) <- c("Tissue","Factor","Antibody","Condition_1","Condition_2","Replicate","InputToUse","toMerge")
  Metadata <- matrix(nrow=length(GenomicsID),ncol=ncol(TempMetaData))  
  Metadata[1:nrow(SS),] <- as.matrix(TempMetaData)
  colnames(Metadata) <- colnames(SS)[colnames(SS) %in% c("Tissue","Factor","Antibody","Condition_1","Condition_2","Replicate","InputToUse","toMerge")]
  Source_File <- c(SS[match(SS[,1],SS_Samples),"Source_File"],rep("NA",length(as.vector(NewLimsFrame[,1]))))
  Processed_bamFileName <- c(SS[match(SS[,1],SS_Samples),"Processed_bamFileName"],rep("NA",length(as.vector(NewLimsFrame[,1]))))
  if(nrow(SS) > 1){
   TempResdata <- as.matrix(SS[,colnames(SS) %in% c("Original","delRand","Excluded","Filtered","Unique","DuplicationRate","InputFileNameToUse","derivedFrom","Macs_Fragment_Length","Sissr_Fragment_Length","Correlation_Fragment_Length","Coverage_Fragment_Length")],ncol=12)
  }else{  
   TempResdata <- matrix(SS[,colnames(SS) %in% c("Original","delRand","Excluded","Filtered","Unique","DuplicationRate","InputFileNameToUse","derivedFrom","Macs_Fragment_Length","Sissr_Fragment_Length","Correlation_Fragment_Length","Coverage_Fragment_Length")],ncol=12)  
  }
    colnames(TempResdata) <- c("Original","delRand","Excluded","Filtered","Unique","DuplicationRate","InputFileNameToUse","derivedFrom","Macs_Fragment_Length","Sissr_Fragment_Length","Correlation_Fragment_Length","Coverage_Fragment_Length")


  Resdata <- matrix(nrow=length(GenomicsID),ncol=ncol(TempResdata))
  Resdata[1:nrow(SS),] <- as.matrix(TempResdata)
  colnames(Resdata) <- colnames(SS)[colnames(SS) %in% c("Original","delRand","Excluded","Filtered","Unique","DuplicationRate","InputFileNameToUse","derivedFrom","Macs_Fragment_Length","Sissr_Fragment_Length","Correlation_Fragment_Length","Coverage_Fragment_Length")]
  
  CleanedSheet <- cbind(GenomicsID,SampleName,Run,Lane,Analysis_State,BamLocation,FQLocation,BarcodesFiles,Metadata,Source_File,Processed_bamFileName,Resdata)
  CleanedSheet <- CleanedSheet[!is.na(CleanedSheet[,"GenomicsID"]),]
  GoodNames <- GetUsernames()
  CleanedSheet[,"SampleName"] <- gsub(" ","_",CleanedSheet[,"SampleName"])
  CleanedSheet[,"BamLocation"] <- gsub("solexa/solexa","solexa",CleanedSheet[,"BamLocation"])
  CleanedSheet[,"FQLocation"] <- gsub("solexa/solexa","solexa",CleanedSheet[,"FQLocation"])
  CleanedSheet[is.na(CleanedSheet[,"FQLocation"]) | CleanedSheet[,"FQLocation"] %in% "/","FQLocation"] <-  "Location_Not_Known"
  CleanedSheet[is.na(CleanedSheet[,"BamLocation"]) | CleanedSheet[,"BamLocation"] %in% "/" | CleanedSheet[,"BamLocation"] %in% "NA/NA","BamLocation"] <-  "Location_Not_Known"
  CleanedSheet[,"BamLocation"] <- gsub("archive.crnet.org",GoodNames[[2]],as.vector(CleanedSheet[,"BamLocation"]),fixed=T)
  CleanedSheet[,"FQLocation"] <- gsub("archive.crnet.org",GoodNames[[2]],as.vector(CleanedSheet[,"FQLocation"]),fixed=T)
  
  CleanedSheet <- CleanedSheet[!apply(CleanedSheet,1,function(x)all(is.na(x))),]
  if(nrow(UpdateLimsFrame) > 0){
    for(i in 1:nrow(UpdateLimsFrame)){
        if(!is.na(UpdateLimsFrame[i,6]) & !UpdateLimsFrame[i,6] %in% "NA"){
            CleanedSheet[CleanedSheet[,1] %in% UpdateLimsFrame[i,1],"BamLocation"] <- UpdateLimsFrame[i,6]
        }  
        if(!is.na(UpdateLimsFrame[i,7]) & !UpdateLimsFrame[i,7] %in% "NA"){
            CleanedSheet[CleanedSheet[,1] %in% UpdateLimsFrame[i,1],"FQLocation"] <- UpdateLimsFrame[i,7]
        }            
          
        }
    }


  return(CleanedSheet)
}


RunSSfetchPipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipeLineLocations,Config="Config"){
  
  Variables  <- file.path(WkgDir,"")
  names(Variables) <- "WorkingDirectory"
  #& SampleSheet[,"Analysis_State"] %in% "RunMe" 
  ToFetch <- SampleSheet[SampleSheet[,"BamLocation"] != "Location_Not_Known"  & SampleSheet[,"Analysis_State"] %in% "RunMe" & SampleSheet[,"BamLocation"] != "Premultiplex" & !is.na(SampleSheet[,"BamLocation"]) & SampleSheet[,"BamLocation"] != "/" & SampleSheet[,"BamLocation"] != "NA",c("GenomicsID","BamLocation")]
  if(length(ToFetch) > 0){
      Specialisations  <- vector("list",length=nrow(ToFetch))
      for(i in 1:length(Specialisations)){
        Specialisations[[i]] <- c(as.vector(ToFetch[i,"BamLocation"]),as.vector(ToFetch[i,"GenomicsID"]))
        names(Specialisations[[i]]) <- c("BamToGet","Bam_Name")  
      } 
      names(Specialisations) <-  paste(as.vector(ToFetch[,"GenomicsID"]),"_fetch",sep="") 
      Pipeline = "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/BamGettingPipeline.xml"  
      PipeName <- "SS_Fetch"
      SSfetch_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
      saveXML(SSfetch_WfMeta,file=file.path(PLs@WorkFlowDir,"SSfetch.xml"))
      system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"SSfetch.xml"),sep=""),wait=T)
  }
  BamFilesReceived <- dir(path=PLs@BamDir,pattern="*.bam$")
  if(any(grep("Processed",BamFilesReceived))){BamFilesReceived <- BamFilesReceived[-grep("Processed",BamFilesReceived)]}
  if(any(grep("Realign",BamFilesReceived))){BamFilesReceived <- BamFilesReceived[-grep("Realign",BamFilesReceived)]}
  
  #BamID <- gsub(".bam","",gsub("\\.bwa.*","",BamFilesReceived))
  BamID <- BamFilesReceived
  for(i in 1:nrow(SampleSheet)){
  	if(!any(c(grep(".fq.gz$",SampleSheet[i,"Source_File"]),grep(".fq$",SampleSheet[i,"Source_File"]),grep("Processed",SampleSheet[i,"Source_File"]),grep("Realign",SampleSheet[i,"Source_File"])))){
      if(any(BamID %in% basename(SampleSheet[i,"BamLocation"]))){
  			SampleSheet[i,"Source_File"] <- BamID[BamID %in% basename(SampleSheet[i,"BamLocation"])]
  		}
  	}
  }

    SampleSheet <- SampleSheet[!apply(SampleSheet,1,function(x)all(is.na(x))),]

return(SampleSheet)
}



RunCheckGenomePipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipelineLocations,Config="Config"){
   
  genome <- GetGenomeFromConfig(WkgDir,ConfigDirectory=Config)
  Variables  <- c(file.path(WkgDir,""),genome)
  names(Variables) <- c("WorkingDirectory","genome")
  
  ToGenomify <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe" & SampleSheet[,"Source_File"] != "NA" & !is.na(SampleSheet[,"Source_File"]),"Source_File"]
  ToGenomify <- gsub(".bam","",ToGenomify[grep("*.bam",ToGenomify)])
  
  Specialisations  <- list()
  if(length(ToGenomify) > 0){
    for(i in 1:length(ToGenomify)){
      Specialisations[[i]] <- c(as.vector(ToGenomify[i]))
      names(Specialisations[[i]]) <- c("BamName")  
    } 
    names(Specialisations) <-  paste(as.vector(gsub("\\.bwa.*","",ToGenomify)),"_genomeCheck",sep="") 
  
    Pipeline = "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/MultiGenomeGetterPipe.xml"  
    PipeName <- "SS_GenomeCheck"
    SSCheckGenome_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
    saveXML(SSCheckGenome_WfMeta,file=file.path(PLs@WorkFlowDir,"SSCheckGenome.xml"))
    system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"SSCheckGenome.xml"),sep=""),wait=T)
  }
    files <- dir(path=PLs@BamDir,pattern="*.info",full.names = T)
    Genomes <- vector("character",length=length(files))
    fileNames <- gsub(".info","",basename(files))
    SamplesFQToGrab <- vector()
    if(length(files) > 0){
    for(i in 1:length(files)){
  	   Genomes[i] <- as.vector(read.delim(files[i],header=F,sep="\t"))
    }
    Genomes <- as.vector(unlist(Genomes))
    GenomeMat <- cbind(paste(fileNames,".bam",sep=""),Genomes)
   
    GenomesToAlign <- GenomeMat[!tolower(GenomeMat[,2]) %in% tolower(genome),1]
    
    if(length(GenomesToAlign) > 0){
     SamplesFQToGrab <- vector("character",length=length(GenomesToAlign))
     for(i in 1:length(GenomesToAlign)){
      if(any(SampleSheet[,"Source_File"] %in% GenomesToAlign[i])){
  	   SamplesFQToGrab[i] <- as.vector(SampleSheet[SampleSheet[,"Source_File"] %in% GenomesToAlign[i],1])
       if(length(as.vector(SampleSheet[SampleSheet[,"Source_File"] %in% GenomesToAlign[i],1])) > 1){
       print(as.vector(SampleSheet[SampleSheet[,"Source_File"] %in% GenomesToAlign[i],1]))
       print(GenomesToAlign[i])
       print("")
       }
      }	
     }
    }
  }

  return(SamplesFQToGrab)
}


RunSSfqfetchPipeline <- function(SampleToGrab=BamsToRealign,SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipeLineLocations,Config="Config"){

  
  genome <- GetGenomeFromConfig(WkgDir,ConfigDirectory=Config)
  Variables  <- c(file.path(WkgDir,""))
  names(Variables) <- c("WorkingDirectory")
  
  ToGrab <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe" & SampleSheet[,"GenomicsID"] %in% SampleToGrab & !SampleSheet[,"FQLocation"] %in% "Location_Not_Known","FQLocation"]
  NameToGrab <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe" & SampleSheet[,"GenomicsID"] %in% SampleToGrab  & !SampleSheet[,"FQLocation"] %in% "Location_Not_Known","GenomicsID"]
  
  FindThoseToBePlexed <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe" & SampleSheet[,"GenomicsID"] %in% unique(as.vector(SampleSheet[,"Source_File"])),"FQLocation"] 
  FindThoseNamesOfBeingPlexed <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe" & SampleSheet[,"GenomicsID"] %in% unique(as.vector(SampleSheet[,"Source_File"])),"GenomicsID"]
  
  ThoseWithFQNoBam <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe" & SampleSheet[,"BamLocation"] %in% "Location_Not_Known" & !SampleSheet[,"FQLocation"] %in% "Location_Not_Known","FQLocation"]
  NamesOfThoseWithFQNoBam <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe" & SampleSheet[,"BamLocation"] %in% "Location_Not_Known" & !SampleSheet[,"FQLocation"] %in% "Location_Not_Known","GenomicsID"]
  
  FindThoseNamesOfBeingPlexed <-   FindThoseNamesOfBeingPlexed[!is.na(FindThoseToBePlexed)]
  FindThoseToBePlexed <-   FindThoseToBePlexed[!is.na(FindThoseToBePlexed)]
  
  ThoseWithFQNoBam <-   ThoseWithFQNoBam[!is.na(ThoseWithFQNoBam)]
  NamesOfThoseWithFQNoBam <-   NamesOfThoseWithFQNoBam[!is.na(ThoseWithFQNoBam)]
  
  ToGrab <- c(ToGrab,FindThoseToBePlexed,ThoseWithFQNoBam)
  NameToGrab <- c(NameToGrab,FindThoseNamesOfBeingPlexed,NamesOfThoseWithFQNoBam)
  
  if(length(ToGrab) > 0){
    
    Specialisations  <- vector("list",length=length(ToGrab))
    for(i in 1:length(Specialisations)){
      Specialisations[[i]] <- c(as.vector(ToGrab[i]),as.vector(NameToGrab[i]))
      names(Specialisations[[i]]) <- c("BamToGet","Bam_Name")  
    } 
    names(Specialisations) <-  paste(as.vector(gsub("\\.bwa.*","",NameToGrab)),"_FQFetch",sep="") 
  
    Pipeline = "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/FQGettingPipeline.xml"  
    PipeName <- "SS_FQFetch"
    SSFQFetch_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
    saveXML(SSFQFetch_WfMeta,file=file.path(PLs@WorkFlowDir,"SSFQFetch.xml"))
    system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"SSFQFetch.xml"),sep=""),wait=T)
  }
  FQFilesDirs <- list.dirs(path=PLs@FQDir,recursive=F)
  BaseFQDirs  <-  basename(FQFilesDirs)
  for(i in 1:nrow(SampleSheet)){
  	if(!any(c(grep(".fq.gz$",SampleSheet[i,"Source_File"]),grep(".fq$",SampleSheet[i,"Source_File"]),grep("Processed",SampleSheet[i,"Source_File"]),grep("Realign",SampleSheet[i,"Source_File"])))){
      if(any(SampleToGrab %in% SampleSheet[i,"GenomicsID"])){
        if(any(BaseFQDirs %in% SampleSheet[i,"GenomicsID"])){
#  			SampleSheet[i,"Source_File"] <- SampleSheet[i,"FQLocation"][basename(SampleSheet[i,"FQLocation"]) %in% basename(dir(FQFilesDirs[BaseFQDirs %in% SampleSheet[i,"GenomicsID"]],full.names=T))]
        TempSampleFQbases <- basename(dir(FQFilesDirs[BaseFQDirs %in% SampleSheet[i,"GenomicsID"]],full.names=T))
        if(length(dir(FQFilesDirs[BaseFQDirs %in% SampleSheet[i,"GenomicsID"]],full.names=T)) > 0){
          SampleSheet[i,"Source_File"] <- dir(FQFilesDirs[BaseFQDirs %in% SampleSheet[i,"GenomicsID"]],full.names=T)[TempSampleFQbases %in% basename(SampleSheet[i,"FQLocation"])]
        }
  		}else{
  		    SampleSheet[i,"Source_File"] <- "NA"
  		    SampleSheet[i,"Analysis_State"] <- "FQ required!!"
  		}	
  	 }
    }
  }
  return(SampleSheet)  
}

RunSSdeMultiplesPipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipeLineLocations,Config="Config"){

    DemultiplexSS <- SampleSheet[SampleSheet[,"BamLocation"] %in% "Premultiplex",]
    AllSourceFiles <- as.vector(DemultiplexSS[,"Source_File"])    
    dir.create(file.path(PLs@FQDir),showWarnings = FALSE)
    Specialisations  <- vector("list")
    if(nrow(DemultiplexSS) > 0){

    for(i in 1:length(unique(DemultiplexSS[,"Source_File"]))){
      FQwhichBarcodesFrom <- as.vector(SampleSheet[SampleSheet[,"GenomicsID"] %in% unique(DemultiplexSS[,"Source_File"])[i],"Source_File"])
      SequenceExpected <-  as.vector(SampleSheet[SampleSheet[,"GenomicsID"] %in% unique(DemultiplexSS[,"Source_File"])[i],"GenomicsID"])
      FQDir <- file.path(PLs@FQDir,as.vector(SampleSheet[SampleSheet[,"GenomicsID"] %in% unique(DemultiplexSS[,"Source_File"])[i],"GenomicsID"]),"")
      print(FQwhichBarcodesFrom)
      print(SequenceExpected)
      print(FQDir)
      if(length(FQwhichBarcodesFrom) > 0 & length(SequenceExpected) > 0 & length(FQDir) > 0){    
        if(grep("^SLX",SequenceExpected) > 0){
          BarcodeFile <- dir(path=file.path(PLs@LocationsDir,"barcodes"),full.names=T)[gsub(".txt$","",dir(path=file.path(PLs@LocationsDir,"barcodes"),full.names=F)) %in% gsub("\\..*","",SequenceExpected)]
        }else{
          BarcodeFile <- dir(path=file.path(PLs@LocationsDir,"barcodes"),full.names=T)[gsub(".txt$","",dir(path=file.path(PLs@LocationsDir,"barcodes"),full.names=F)) %in% SequenceExpected]
     
        }      
  
        Specialisations <- c(Specialisations,list(c(FQwhichBarcodesFrom,SequenceExpected,FQDir,BarcodeFile)))
        names(Specialisations[[length(Specialisations)]]) <- c("SequenceFile","SequenceExpected","demuxDir","indexFile")
      }
      
      }
      names(Specialisations) <- seq(1,length(Specialisations))
      Variables <- WkgDir
      names(Variables) <- "WorkingDirectory"
      Pipeline <- "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/SpecialisationDemultiplex.xml"
      PipeName <- "Demultiplex"
      SSDemultiplex_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
      saveXML(SSDemultiplex_WfMeta,file=file.path(PLs@WorkFlowDir,"SSDemultiplex.xml"))
      system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"SSDemultiplex.xml"),sep=""),wait=T) 
      }       
      for(i in 1:nrow(SampleSheet)){
        if(SampleSheet[i,"FQLocation"] %in% "Premultiplex"){
          AllFileForSample <- dir(path=file.path(PLs@FQDir,SampleSheet[i,"Source_File"]),pattern=".gz$")
          AllFileForSampleFull <- dir(path=file.path(PLs@FQDir,SampleSheet[i,"Source_File"]),pattern=".gz$",full.names=T)
          SampleSheet[i,"FQLocation"] <- AllFileForSampleFull[gsub(".gz$","",AllFileForSample) %in% SampleSheet[i,"SampleName"]]
          SampleSheet[i,"Source_File"] <- AllFileForSampleFull[gsub(".gz$","",AllFileForSample) %in% SampleSheet[i,"SampleName"]]          
          SampleSheet[i,"BamLocation"] <- "Pre-Alignment"
        }
      }
      return(SampleSheet)
}

RunSSRealignmentPipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipeLineLocations,Config="Config"){
  RemainderSheet <- SampleSheet[!SampleSheet[,"Analysis_State"] %in% "RunMe",]
  SampleSheet <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe",]
  SampleSheetOfThoseToAlign <- SampleSheet[grep("fq$|.gz$|.gz$",SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe","Source_File"]),]
  SampleSheetOfThoseToAlign <- SampleSheetOfThoseToAlign[!is.na(SampleSheetOfThoseToAlign[,"Source_File"]) & !SampleSheetOfThoseToAlign[,"Source_File"] %in% "NA",]
  TargetGenome <- GetGenomeFromConfig(WkgDir,ConfigDirectory="Config")
  GenomeBuild <- GetGenomeBuildFromConfig(WkgDir,ConfigDirectory="Config")
  Specialisations  <- vector("list")
  
  Variables <- c(WkgDir,PLs@FQDir,TargetGenome,GenomeBuild)
  names(Variables) <- c("WorkingDirectory","FQDirectory","GenomeBuild","Genome")
  if(nrow(SampleSheetOfThoseToAlign) > 0){ 
    for(i in 1:nrow(SampleSheetOfThoseToAlign)){
      SampleID <- SampleSheetOfThoseToAlign[i,"GenomicsID"]
      FileToAlign <- SampleSheetOfThoseToAlign[i,"Source_File"]
      Specialisations <- c(Specialisations,list(c(SampleID,FileToAlign)))
      names(Specialisations[[length(Specialisations)]]) <- c("Test","FastaFile")  
    }
     names(Specialisations) <- seq(1,length(Specialisations))  
     Pipeline <- "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/ReAlignPipeline.xml"
     PipeName <- "Alignment"
     SSRealignment_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
     saveXML(SSRealignment_WfMeta,file=file.path(PLs@WorkFlowDir,"SSAlignment.xml"))
     system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"SSAlignment.xml"),sep=""),wait=T)
   }
   for(i in 1:nrow(SampleSheet)){
     if(file.exists(file.path(PLs@BamDir,paste(SampleSheet[i,"GenomicsID"],".bwa.Realigned",TargetGenome,".bam",sep="")))){
        SampleSheet[i,"Source_File"] <- paste(SampleSheet[i,"GenomicsID"],".bwa.Realigned",TargetGenome,".bam",sep="") 
     }
   }
      SampleSheet <- rbind(SampleSheet,RemainderSheet)
   
   return(SampleSheet)
     
}

RunSSMergingPipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipeLineLocations,Config="Config"){
    UniquedMergedFiles <-  as.vector(unique(na.omit(SampleSheet[,"toMerge"])))
    MoreThan1Sample <- names(table(na.omit(SampleSheet[,"SampleName"])))[table(na.omit(SampleSheet[,"SampleName"])) > 1]
    AllSamplesPresent <- as.vector(unique(na.omit(SampleSheet[,"GenomicsID"])))


 
    
    UniquedMergedFiles <- UniquedMergedFiles[!UniquedMergedFiles %in% c(AllSamplesPresent)]
    UniquedMergedFiles <- UniquedMergedFiles[!UniquedMergedFiles %in% MoreThan1Sample]
         
    InputLists <- vector("character")
    InputLists2 <- vector("character")
    OutputNames <- vector("character")          
    OutputNames2 <- vector("character")    
    if(length(UniquedMergedFiles) != 0){
 
      OutputNames <- UniquedMergedFiles
      for(i in 1:length(UniquedMergedFiles)){
          if(length(grep("*.bam$",as.vector(SampleSheet[SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe","toMerge"] %in% UniquedMergedFiles[i],"Source_File"]))) > 1){
            ToBeMerged <-  as.vector(SampleSheet[SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe","toMerge"] %in% UniquedMergedFiles[i],"Source_File"])
            ToBeMerged <- ToBeMerged[!is.na(ToBeMerged)]
            ToBeMerged <- ToBeMerged[!grepl(".fq$|fq.gz$",ToBeMerged)]
            if(length(ToBeMerged) > 0){
              InputLists[i] <- paste(paste("INPUT=",file.path(PLs@BamDir,ToBeMerged),sep=""),collapse=" ")
            }
          }
      }
      UniquedMergedFiles <- UniquedMergedFiles[!gsub("INPUT=","",InputLists) == ""]   
    }
    
    MoreThan1Sample <- MoreThan1Sample[!MoreThan1Sample %in% c(AllSamplesPresent)]  
    
    if(length(MoreThan1Sample) != 0){
      InputLists2 <- vector("character",length=length(MoreThan1Sample))
      OutputNames2 <- MoreThan1Sample
      for(i in 1:length(MoreThan1Sample)){
        if(length(grep("*.bam$",as.vector(SampleSheet[SampleSheet[,"SampleName"] %in% MoreThan1Sample[i],"Source_File"]))) > 1){
          ToBeMerged2 <-  as.vector(SampleSheet[SampleSheet[,"SampleName"] %in% MoreThan1Sample[i],"Source_File"])
          ToBeMerged2 <- ToBeMerged2[!is.na(ToBeMerged2)]
          ToBeMerged2 <- ToBeMerged2[!grepl(".fq$|fq.gz$",ToBeMerged2)] 
          if(length(ToBeMerged2) > 0){
             InputLists2[i] <- paste(paste("INPUT=",file.path(PLs@BamDir,ToBeMerged2),sep=""),collapse=" ")
          }         
        }
      }
      MoreThan1Sample <- MoreThan1Sample[!gsub("INPUT=","",InputLists2) == ""]
    }
    InputLists <- c(InputLists,InputLists2)
    OutputNames <- c(OutputNames,OutputNames2)
    Variables <- c(WkgDir)
    names(Variables) <- c("WorkingDirectory")
    Specialisations  <- vector("list")
  if(length(InputLists) > 0){       
    for(i in 1:length(InputLists)){   
        Inputs <- InputLists[i]
         if(Inputs != ""){
        Outputs <- OutputNames[i]
        Specialisations <- c(Specialisations,list(c(Inputs,Outputs)))
        names(Specialisations[[length(Specialisations)]]) <- c("InputName","OutName")  
      }
    }
   
     names(Specialisations) <- seq(1,length(Specialisations))  
     Pipeline <- "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/MergingPipeline.xml"
     PipeName <- "Merging"
     SSMerge_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
     saveXML(SSMerge_WfMeta,file=file.path(PLs@WorkFlowDir,"SSMerging.xml"))
     system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"SSMerging.xml"),sep=""),wait=T)
     TempMatrix <- matrix(nrow=length(OutputNames),ncol=ncol(SampleSheet),data=NA)
     colnames(TempMatrix) <- colnames(SampleSheet)
     for(i in 1:length(OutputNames)){
         if(file.exists(file.path(PLs@BamDir,paste(OutputNames[i],".bwa.bam",sep="")))){
            TempMatrix[i,"GenomicsID"] <- OutputNames[i]
            TempMatrix[i,"SampleName"] <- OutputNames[i]
            TempMatrix[i,"BamLocation"] <- file.path(PLs@BamDir,paste(OutputNames[i],".bwa.bam",sep=""))
            TempMatrix[i,"Source_File"] <- paste(OutputNames[i],".bwa.bam",sep="") 
            TempMatrix[i,"Analysis_State"]  <- "RunMe"            
         }
     }
     SampleSheet <- rbind(SampleSheet,TempMatrix[-1,])
     SampleSheet[SampleSheet[,"toMerge"] %in% OutputNames ,"Analysis_State"]  <- "MergedAndUnused"
     SampleSheet[!SampleSheet[,"GenomicsID"] %in% OutputNames & SampleSheet[,"SampleName"] %in% OutputNames & SampleSheet[,"Analysis_State"] %in% "RunMe","Analysis_State"]  <- "MergedAndUnused"
   }
   SampleSheet
}

RunBamProcessPipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipelineLocations,Config="Config"){
   
#  genome <- GetGenomeFromConfig(WkgDir,ConfigDirectory=Config)
  Variables  <- c(file.path(WkgDir,""),PLs@BamDir,"Dummy")
  names(Variables) <- c("WorkingDirectory","BamDirectory","genomeFile")
  
  BamFiles <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe" & !SampleSheet[,"Source_File"] %in% "NA" & !is.na(SampleSheet[,"Source_File"]) & grepl(".bam",SampleSheet[,"Source_File"]) & !grepl("_Processed.bam",SampleSheet[,"Source_File"]),"Source_File"]
  BamFiles <- gsub(".bam","",BamFiles)
  
  genome <- GetGenomeFromConfig(WkgDir,ConfigDirectory="Config")
  excludedList <- GetExcludedListFromConfig(WkgDir,ConfigDirectory="Config")
  SeqDict <- GetSequenceDictionaryFromConfig(WkgDir,ConfigDirectory="Config")
  
  if(length(BamFiles) > 0){
    Specialisations  <- vector("list",length=length(BamFiles))
    for(i in 1:length(Specialisations)){
      Specialisations[[i]] <- c(as.vector(BamFiles[i]),genome,excludedList,SeqDict)
      names(Specialisations[[i]]) <- c("Test","genome","excludedfile","seqdict")  
    } 
    names(Specialisations) <-  paste(as.vector(gsub("\\.bwa.*","",BamFiles)),"_bamProcess",sep="") 
    #Specialisations[[1]] <- "/lustre/mib-cri/carrol09/Work/PipelinePracticeSet/20121114_MontoyaAR_DN_Hes6ChIP/bamFiles/ID-LNCAP-LM-HES6-BICALUTAMIDE-INPUT-D26.bwa.bam"
  
    Pipeline = "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/BamProcessPipeline.xml"  
    PipeName <- "SS_BamProcess"
    SSBamProcess_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
    saveXML(SSBamProcess_WfMeta,file=file.path(PLs@WorkFlowDir,"SS_BamProcess.xml"))
    system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"SS_BamProcess.xml"),sep=""),wait=T)
  }
  fileLogs <- dir(path=PLs@BamDir,pattern="_fileLog.log",full.names = F)
  fileLogsFull <- dir(path=PLs@BamDir,pattern="_fileLog.log",full.names = T)  
  
  CovAndSisserLogs <- dir(path=PLs@FragLengthDir,pattern="AllFragLog$",full.names = F)
  CovAndSisserLogsFull <- dir(path=PLs@FragLengthDir,pattern="AllFragLog$",full.names = T)

  CorrLogs <- dir(path=PLs@FragLengthDir,pattern="CorrFragLog$",full.names = F)
  CorrLogsFull <- dir(path=PLs@FragLengthDir,pattern="CorrFragLog$",full.names = T)  
  
  
  BamsInSampleSheet <-  as.vector(SampleSheet[,"Source_File"])
  SampleSheet <- data.frame(SampleSheet,stringsAsFactors=F)
  
  for(i in 1:nrow(SampleSheet)){
    if(any(gsub("_fileLog.log","",fileLogs) %in% gsub(".bam","",SampleSheet[i,"Source_File"]))){
       LogToRead <- fileLogsFull[gsub("_fileLog.log","",fileLogs) %in% gsub(".bam","",SampleSheet[i,"Source_File"])]
       DataIn <- read.delim(LogToRead,sep="\t")
       SampleSheet[i,c("Original","delRand","Excluded","Filtered","Unique")] <- DataIn[,c("Mapped","NonRandomChr","IncludedRegions","QC...15","Unique")]
       SampleSheet[i,"DuplicationRate"] <- (as.numeric(SampleSheet[i,"Filtered"])-as.numeric(SampleSheet[i,"Unique"]))/as.numeric(SampleSheet[i,"Filtered"])*100
       SampleSheet[i,"NRF"] <- DataIn[,"Unique"]/DataIn[,"QC...15"]

    }else{
    #  SampleSheet[i,"Source_File"] <- "No_Processed_Bam"
    	SampleSheet[i,c("Original","delRand","Excluded","Filtered","Unique")] <- rep("No_Information_Available",5)
    	SampleSheet[i,"DuplicationRate"] <- "No_Information_Available"
    }
    if(any(gsub("_Processed.AllFragLog","",CovAndSisserLogs) %in% gsub(".bam","",SampleSheet[i,"Source_File"]))){
    		FragsToRead <- CovAndSisserLogsFull[gsub("_Processed.AllFragLog","",CovAndSisserLogs) %in% gsub(".bam","",SampleSheet[i,"Source_File"])]
    		print(FragsToRead)
    		DataIn <- as.matrix(read.delim(FragsToRead,sep=" ",comment.char="#"))
    		SampleSheet[i,"Sissr_Fragment_Length"] <- DataIn[1,1]		
    		SampleSheet[i,"Correlation_Fragment_Length"] <- DataIn[1,2]
    		SampleSheet[i,"Coverage_Fragment_Length"] <- DataIn[1,3]
   	}else{
    		SampleSheet[i,"Sissr_Fragment_Length"] <- "No_Information_Available"		
    		SampleSheet[i,"Correlation_Fragment_Length"] <- "No_Information_Available"
    		SampleSheet[i,"Coverage_Fragment_Length"] <- "No_Information_Available"	
    
   	}
    if(any(gsub("_Processed.CorrFragLog","",CorrLogs) %in% gsub(".bam","",SampleSheet[i,"Source_File"]))){
    		CorrToRead <- CorrLogsFull[gsub("_Processed.CorrFragLog","",CorrLogs) %in% gsub(".bam","",SampleSheet[i,"Source_File"])]
    		print(CorrToRead)
    		DataIn <- as.matrix(read.delim(CorrToRead,sep=" ",comment.char="#"))
    		SampleSheet[i,"CC_Fragment_Length"] <- DataIn[1,1]		
    		SampleSheet[i,"NSC"] <- DataIn[1,2]
    		SampleSheet[i,"RSC"] <- DataIn[1,3]
   	}else{
    		SampleSheet[i,"CC_Fragment_Length"] <- "No_Information_Available"		
    		SampleSheet[i,"NSC"] <- "No_Information_Available"
    		SampleSheet[i,"RSC"] <- "No_Information_Available"	
   	}
   	if(any(gsub("_fileLog.log","",fileLogs) %in% gsub(".bam","",SampleSheet[i,"Source_File"]))){
      if(file.exists(gsub("_fileLog.log$","_Processed.bam",fileLogsFull)[gsub("_fileLog.log$","",fileLogs) %in% gsub(".bam","",SampleSheet[i,"Source_File"])])){
            SampleSheet[i,"Processed_bamFileName"] <- gsub("_fileLog.log$","_Processed.bam",fileLogs)[gsub("_fileLog.log$","",fileLogs) %in% gsub(".bam","",SampleSheet[i,"Source_File"])]
      }else{
                  # SampleSheet[i,"Source_File"] <- "No_Processed_Bam"   
      }
    }
  }
  return(SampleSheet)
}



RunBamProfilePipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipelineLocations,Config="Config"){
   
   genome <- GetGenomeFromConfig(WkgDir,ConfigDirectory="Config") 
   genomeChrLengths <- GetChrLengthsFromConfig(WkgDir,ConfigDirectory="Config")
   
#  genome <- GetGenomeFromConfig(WkgDir,ConfigDirectory=Config)
  Variables  <- c(file.path(WkgDir,""),PLs@BamDir,genome,genomeChrLengths)
  names(Variables) <- c("WorkingDirectory","BamDirectory","genomeName","genomeFile")
  
  BamProfileFiles <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe" & SampleSheet[,"Processed_bamFileName"] != "NA" & !is.na(SampleSheet[,"Processed_bamFileName"]) & grepl(".bam",SampleSheet[,"Processed_bamFileName"]),"Processed_bamFileName"]
  BamProfileFiles <- gsub(".bam","",BamProfileFiles)
  
  Specialisations  <- vector("list",length=length(BamProfileFiles))
  for(i in 1:length(Specialisations)){
    Specialisations[[i]] <- c(as.vector(BamProfileFiles[i]))
    names(Specialisations[[i]]) <- c("Test")  
  } 
  names(Specialisations) <-  paste(as.vector(gsub("\\.bwa.*","",BamProfileFiles)),"_bamProcess",sep="") 

  Pipeline = "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/MultiBamProcessPipeline_P2.xml"  
  PipeName <- "SS_BamProfile"
  SSBamProfile_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
  saveXML(SSBamProfile_WfMeta,file=file.path(PLs@WorkFlowDir,"SS_BamProfile.xml"))
  system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"SS_BamProfile.xml"),sep=""),wait=T)

    SampleSheet <- ReadAndLock(file.path(WkgDir,"SampleSheet.csv"),WkdDir,SAF=T,napTime=5)
  
  for(i in 1:nrow(SampleSheet)){
  	SampleToLookFor <- gsub(".bam","",SampleSheet[i,"Processed_bamFileName"])
  	BedGraphFile <- dir(path=file.path(WkgDir,"Coverage"),pattern=paste(SampleToLookFor,".bedgraph",sep=""),full.names=T)
  	if(length(BedGraphFile) > 0){
  		SampleSheet[i,"BedGraph_Files"] <- BedGraphFile 
  	}
  
  	BigWigFile <- dir(path=file.path(WkgDir,"Coverage"),pattern=paste(SampleToLookFor,".bw",sep=""),full.names=T)
  	if(length(BigWigFile) > 0){
  		SampleSheet[i,"BigWig_Files"] <- BigWigFile
  	}
  
  	CovStats <- dir(path=file.path(WkgDir,"Coverage"),pattern=paste(SampleToLookFor,"_CoverageDispersion.RData",sep=""),full.names=T)
  	if(length(CovStats) > 0){
  		load(CovStats)
  		SampleSheet[i,"SSD_Of_Coverage"] <- ssdOfSample
  		SampleSheet[i,"Gini_Of_Coverage"] <- giniOfSample[1]
  		SampleSheet[i,"adjusted_Gini_Of_Coverage"] <- giniOfSample[2]
  		rm(giniOfSample)
  		rm(ssdOfSample)
  	}
  	ReadsInFeatures <- dir(path=file.path(WkgDir,"Coverage"),pattern=paste(SampleToLookFor,"_ReadCountsInFeatures.txt",sep=""),full.names=T)
  	if(length(CovStats) > 0){
  		Temp <- read.delim(ReadsInFeatures,h=T,sep="\t")
  		if(any(colnames(Temp) %in% "TSS_500Upstream_500Downstream")){
  			Total <- Temp["Counts","Total"]
  			TSS <- Temp["Counts","TSS_500Upstream_500Downstream"]
  			Pro2000_500 <- Temp["Counts","Promoter_2000Upstream_500Upstream"]
  			Pro5000_2000 <- Temp["Counts","Promoter_5000Upstream_2000Upstream"]
  			Pro10000_5000 <- Temp["Counts","Promoter_10000Upstream_5000Upstream"]
  			GeneBody <- Temp["Counts","GeneBody_minus_TSS"]
  			Intragenic <- Total-(TSS + Pro2000_500 + Pro5000_2000 + Pro10000_5000 + GeneBody)
  			TSSPer <- (TSS/Total)*100
  			Pro2000_500Per <- (Pro2000_500/Total)*100
  			Pro5000_2000Per <- (Pro5000_2000/Total)*100
  			Pro10000_5000Per <- (Pro10000_5000/Total)*100
  			GeneBodyPer <- (GeneBody/Total)*100
  			IntragenicPer <- (Intragenic/Total)*100
  SampleSheet[i,c("Reads_In_TSS")] <- TSS
  SampleSheet[i,c("Reads_In_Promoter_2000Up_500Up")] <- Pro2000_500
  SampleSheet[i,c("Reads_In_Promoter_5000Up_2000Up")] <- Pro5000_2000
  SampleSheet[i,c("Reads_In_Promoter_10000Up_5000Up")] <- Pro10000_5000
  SampleSheet[i,c("Reads_In_GeneBody_Minus_TSS")] <- GeneBody
  SampleSheet[i,c("Reads_In_Intragenic")]	<- Intragenic	
  
  SampleSheet[i,c("Percent_Of_Reads_In_TSS")] <- TSSPer
  SampleSheet[i,c("Percent_Of_Reads_In_Promoter_2000Up_500Up")] <- Pro2000_500Per
  SampleSheet[i,c("Percent_Of_Reads_In_Promoter_5000Up_2000Up")] <- Pro5000_2000Per
  SampleSheet[i,c("Percent_Of_Reads_In_Promoter_10000Up_5000Up")] <- Pro10000_5000Per
  SampleSheet[i,c("Percent_Of_Reads_In_GeneBody_Minus_TSS")] <- GeneBodyPer
  SampleSheet[i,c("Percent_Of_Reads_In_Intragenic")]	<- IntragenicPer			
  		}
  	}
  
  }	
  
  WriteAndUnlock(SampleSheet,file.path(WkgDir,"SampleSheet.csv"))	
#write.table(sampleSheet,file=file.path(WkgDir,"SampleSheet.csv"),row.names=F,sep=",")

  
}

RunMacsPeakCallerPipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipelineLocations,Config="Config"){
   
   
   Mfold <- getMacsmfold(WkgDir,ConfigDirectory="Config")
   MacsGenome = getMacsGenome(WkgDir,ConfigDirectory="Config")
   ShiftSizeDefault = getMacsShiftSize(WkgDir,ConfigDirectory="Config")    
   

   
   genome <- GetGenomeFromConfig(WkgDir,ConfigDirectory="Config") 
   #genomeChrLengths <- GetChrLengthsFromConfig(WkgDir,ConfigDirectory="Config")
   SampleSheet <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe",]
   SamplesAndInputs <- matrix(data=c(gsub("\\.bam","",SampleSheet[,"Processed_bamFileName"]),gsub("\\.bam","",SampleSheet[match(SampleSheet[,"InputToUse"],SampleSheet[,"SampleName"],incomparable=NA),"Processed_bamFileName"]),as.vector(SampleSheet[,"CC_Fragment_Length"])),ncol=3,byrow=F,dimnames=list(NULL,c("Samples","Inputs","CC_Fraglength")))
   SamplesAndInputs <- SamplesAndInputs[!is.na(SamplesAndInputs[,"Inputs"]) & ! as.vector(SamplesAndInputs[,"Samples"]) %in% "No_Processed_Bam",]

   SamplesAndInputs2 <- matrix(data=c(gsub("\\.bam","",SampleSheet[,"Processed_bamFileName"]),gsub("\\.bam","",SampleSheet[match(SampleSheet[,"InputToUse"],SampleSheet[,"GenomicsID"],incomparable=NA),"Processed_bamFileName"]),as.vector(SampleSheet[,"CC_Fragment_Length"])),ncol=3,byrow=F,dimnames=list(NULL,c("Samples","Inputs","CC_Fraglength")))  
   SamplesAndInputs2 <- SamplesAndInputs2[!is.na(SamplesAndInputs2[,"Inputs"]) & ! as.vector(SamplesAndInputs2[,"Samples"]) %in% "No_Processed_Bam",]

   MakeUniquePeakCalls <- rbind(SamplesAndInputs,SamplesAndInputs2)
   MakeUniquePeakCalls <- MakeUniquePeakCalls[match(unique(MakeUniquePeakCalls[,1]),MakeUniquePeakCalls[,1]),]   
   SamplesAndInputs <- matrix(MakeUniquePeakCalls,ncol=3,byrow=F)

   SamplesAndInputs[SamplesAndInputs[,3] %in% "Too_few_Reads_To_Calculate" | SamplesAndInputs[,3] %in% "No_Information_Available" |  is.na(SamplesAndInputs[,3]),3] <-  ShiftSizeDefault

  Variables  <- c(file.path(PLs@MacsDir,""),file.path(PLs@BamDir,""),MacsGenome,Mfold)
  names(Variables) <- c("Macs_Directory","BamDirectory","Genome","Mfold")
  

  if(nrow(SamplesAndInputs) > 0){
    Specialisations  <- vector("list",length=nrow(SamplesAndInputs))
    for(i in 1:length(Specialisations)){
      Specialisations[[i]] <- c(as.vector(SamplesAndInputs[i,1]),as.vector(SamplesAndInputs[i,2]),as.vector(SamplesAndInputs[i,3]))
      names(Specialisations[[i]]) <- c("Test","Control","shiftSize")  
    } 
    names(Specialisations) <-  paste(as.vector(SamplesAndInputs[,1]),"_MacsPeakCall",sep="") 
  
    Pipeline = "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/MacsPeakCallingPipeline.xml"  
    PipeName <- "SS_MacsPeak"
    SSMacsPeak_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
    saveXML(SSMacsPeak_WfMeta,file=file.path(PLs@WorkFlowDir,"SS_MacsPeak.xml"))
    system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"SS_MacsPeak.xml"),sep=""),wait=T)
}


  MacsFiles <- dir(path=PLs@MacsDir,pattern="*peaks.xls$",full.name=F)
  MacsFilesFull <- dir(path=PLs@MacsDir,pattern="*peaks.xls$",full.name=T)  
  PositivePeaks <- MacsFiles[-grep("negative_peaks.xls",MacsFiles)]
  PositivePeaksFull <- MacsFilesFull[-grep("negative_peaks.xls",MacsFilesFull)]

  SampleSheet <- ReadAndLock(file.path(WkgDir,"SampleSheet.csv"),WkdDir,SAF=F,napTime=5)

  for(i in 1:nrow(SampleSheet)){
	 if(gsub(".bam","",SampleSheet[i,"Processed_bamFileName"]) %in% gsub("_peaks.xls","",PositivePeaks)){
	   	MacsToRead <- file.path(PLs@MacsDir,PositivePeaks[gsub("_peaks.xls","",PositivePeaks) %in% gsub(".bam","",SampleSheet[i,"Processed_bamFileName"])])
 	    print(MacsToRead)
		  DataIn <- read.delim(MacsToRead,sep="\t",comment.char="#")
		  SampleSheet[i,"MacsPeaks"] <- nrow(DataIn)		
		  SampleSheet[i,"Macs_name"] <- MacsToRead
		  print(nrow(DataIn))
		  if(file.exists(gsub("_peaks.xls","_model.r",MacsToRead))){
		    TempForFragLengths <- readLines(gsub("_peaks.xls","_model.r",MacsToRead))
		    SampleSheet[i,"Macs_Fragment_Length"] <- gsub("d=","",gsub("\".*","",gsub(".*right\",c\\(\"d=","",TempForFragLengths[grep("d=",TempForFragLengths)])))
		  }else{
		     SampleSheet[i,"Macs_Fragment_Length"] <- "Default:No_Model"
		     write.table("d=Default:No_Model",gsub("_peaks.xls","_model.r",MacsToRead),sep="",row.names=F,col.names=F,quote=T)
		     write.table("Dummy",gsub("_peaks.xls","_model.pdf",MacsToRead),sep="",row.names=F)		     
		  }
	}
}
        WriteAndUnlock(SampleSheet,file.path(WkgDir,"SampleSheet.csv"))	

        return(SampleSheet)
}

RunSicerPeakCallerPipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipelineLocations,Config="Config"){
   
   
   WindowSize <- getSicerWindowSize(WkgDir,ConfigDirectory="Config")
   GapSize <- getSicerGapSize(WkgDir,ConfigDirectory="Config")      
   GenomeForSicer <- GetGenomeFromConfig(WkgDir,ConfigDirectory="Config")
   GenomeForSicer[tolower(GenomeForSicer) == "grch37"] <- "hg19" 
   GenomeForSicer[GenomeForSicer == "grch37"] <- "mm9"
  #"hg18","GRCh37","hg19","MM9","mm9"
   sicerexec <- getSicerExec(WkgDir,ConfigDirectory="Config")
   
   ShiftSizeDefault <- 100
   
   dir.create(file.path(PLs@SicerDir,""),recursive=T,showWarnings=F)
    
   #genomeChrLengths <- GetChrLengthsFromConfig(WkgDir,ConfigDirectory="Config")
   SampleSheet <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe",]
   SamplesAndInputs <- matrix(data=c(gsub("\\.bam","",SampleSheet[,"Processed_bamFileName"]),gsub("\\.bam","",SampleSheet[match(SampleSheet[,"InputToUse"],SampleSheet[,"SampleName"],incomparable=NA),"Processed_bamFileName"]),as.vector(SampleSheet[,"CC_Fragment_Length"])),ncol=3,byrow=F,dimnames=list(NULL,c("Samples","Inputs","CC_Fraglength")))
   SamplesAndInputs <- SamplesAndInputs[!is.na(SamplesAndInputs[,"Inputs"]) & ! as.vector(SamplesAndInputs[,"Samples"]) %in% "No_Processed_Bam",]

   SamplesAndInputs2 <- matrix(data=c(gsub("\\.bam","",SampleSheet[,"Processed_bamFileName"]),gsub("\\.bam","",SampleSheet[match(SampleSheet[,"InputToUse"],SampleSheet[,"GenomicsID"],incomparable=NA),"Processed_bamFileName"]),as.vector(SampleSheet[,"CC_Fragment_Length"])),ncol=3,byrow=F,dimnames=list(NULL,c("Samples","Inputs","CC_Fraglength")))  
   SamplesAndInputs2 <- SamplesAndInputs2[!is.na(SamplesAndInputs2[,"Inputs"]) & ! as.vector(SamplesAndInputs2[,"Samples"]) %in% "No_Processed_Bam",]

   MakeUniquePeakCalls <- rbind(SamplesAndInputs,SamplesAndInputs2)
   MakeUniquePeakCalls <- MakeUniquePeakCalls[match(unique(MakeUniquePeakCalls[,1]),MakeUniquePeakCalls[,1]),]   
   SamplesAndInputs <- matrix(MakeUniquePeakCalls,ncol=3,byrow=F)

   SamplesAndInputs[SamplesAndInputs[,3] %in% "Too_few_Reads_To_Calculate" | SamplesAndInputs[,3] %in% "No_Information_Available" | is.na(SamplesAndInputs[,3]),3] <-  ShiftSizeDefault

  Variables  <- c(file.path(PLs@SicerDir,""),file.path(PLs@BamDir,""),WkgDir,GenomeForSicer,sicerexec,WindowSize,GapSize,0.01)
  names(Variables) <- c("SicerDirectory","BamDirectory","WorkingDirectory","GS","sicerexec","Window","GapSize","FDR")
  

  if(nrow(SamplesAndInputs) > 0){
    Specialisations  <- vector("list",length=nrow(SamplesAndInputs))
    for(i in 1:length(Specialisations)){
      Specialisations[[i]] <- c(as.vector(SamplesAndInputs[i,1]),as.vector(SamplesAndInputs[i,2]),as.vector(SamplesAndInputs[i,3]))
      names(Specialisations[[i]]) <- c("Test","Control","SS")  
    } 
    names(Specialisations) <-  paste(as.vector(SamplesAndInputs[,1]),"_SicerPeakCall",sep="") 
  
    Pipeline = "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/SicerPeakCallingPipeline.xml"  
    PipeName <- "SS_SicerPeak"
    SSSicerPeak_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
    saveXML(SSSicerPeak_WfMeta,file=file.path(PLs@WorkFlowDir,"SS_SicerPeak.xml"))
    system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"SS_SicerPeak.xml"),sep=""),wait=T)
  }

 #######################
#  MacsFiles <- dir(path=PLs@MacsDir,pattern="*peaks.xls$",full.name=F)
#  MacsFilesFull <- dir(path=PLs@MacsDir,pattern="*peaks.xls$",full.name=T)  
#  PositivePeaks <- MacsFiles[-grep("negative_peaks.xls",MacsFiles)]
#  PositivePeaksFull <- MacsFilesFull[-grep("negative_peaks.xls",MacsFilesFull)]

  SampleSheet <- ReadAndLock(file.path(WkgDir,"SampleSheet.csv"),WkdDir,SAF=F,napTime=5)

  SicerPeaksFull <-  dir(path=PLs@SicerDir,recursive=T,full.names=T)
    
  for(i in 1:nrow(SampleSheet)){
	 if(any(file.path(PLs@SicerDir,gsub(".bam","",SampleSheet[i,"Processed_bamFileName"]),paste(gsub(".bam","",SampleSheet[i,"Processed_bamFileName"]),"-W",WindowSize,"-G",GapSize,"-islands-summary-FDR0.01",sep="")) %in% SicerPeaksFull)){
	   	SicerToRead <- SicerPeaksFull[SicerPeaksFull %in% file.path(PLs@SicerDir,gsub(".bam","",SampleSheet[i,"Processed_bamFileName"]),paste(gsub(".bam","",SampleSheet[i,"Processed_bamFileName"]),"-W",WindowSize,"-G",GapSize,"-islands-summary-FDR0.01",sep=""))]
 	    print(SicerToRead)
		  DataIn <- read.delim(SicerToRead,sep="\t",comment.char="#")
		  SampleSheet[i,"SicerPeaks"] <- nrow(DataIn)		
		  SampleSheet[i,"Sicer_name"] <- SicerToRead
		  print(nrow(DataIn))
    }
  }
        WriteAndUnlock(SampleSheet,file.path(WkgDir,"SampleSheet.csv"))	
        return(SampleSheet)
}
###########################




RunTPICsPeakCallerPipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipelineLocations,Config="Config"){
   
   
   min_size <- getTPICSminSize(WkgDir,ConfigDirectory="Config")
   WideScale <- getTPICSwideSize(WkgDir,ConfigDirectory="Config")
   GenomeForTPICS <- GetGenomeFromConfig(WkgDir,ConfigDirectory="Config")
#   GenomeForTPICS[GenomeForTPICS == "grch37"] <- "hg19" 
#   GenomeForSicer[GenomeForSicer == "grch37"] <- "mm9"

       
   
   ShiftSizeDefault <- 100
   
   dir.create(file.path(PLs@TPICsDir,""),recursive=T,showWarnings=F)
    
   #genomeChrLengths <- GetChrLengthsFromConfig(WkgDir,ConfigDirectory="Config")
   SampleSheet <- SampleSheet[SampleSheet[,"Analysis_State"] %in% "RunMe",]
   SamplesAndInputs <- matrix(data=c(gsub("\\.bam","",SampleSheet[,"Processed_bamFileName"]),gsub("\\.bam","",SampleSheet[match(SampleSheet[,"InputToUse"],SampleSheet[,"SampleName"],incomparable=NA),"Processed_bamFileName"]),as.vector(SampleSheet[,"CC_Fragment_Length"])),ncol=3,byrow=F,dimnames=list(NULL,c("Samples","Inputs","CC_Fraglength")))
   SamplesAndInputs <- SamplesAndInputs[!is.na(SamplesAndInputs[,"Inputs"]) & ! as.vector(SamplesAndInputs[,"Samples"]) %in% "No_Processed_Bam",]

   SamplesAndInputs2 <- matrix(data=c(gsub("\\.bam","",SampleSheet[,"Processed_bamFileName"]),gsub("\\.bam","",SampleSheet[match(SampleSheet[,"InputToUse"],SampleSheet[,"GenomicsID"],incomparable=NA),"Processed_bamFileName"]),as.vector(SampleSheet[,"CC_Fragment_Length"])),ncol=3,byrow=F,dimnames=list(NULL,c("Samples","Inputs","CC_Fraglength")))  
   SamplesAndInputs2 <- SamplesAndInputs2[!is.na(SamplesAndInputs2[,"Inputs"]) & ! as.vector(SamplesAndInputs2[,"Samples"]) %in% "No_Processed_Bam",]

   MakeUniquePeakCalls <- rbind(SamplesAndInputs,SamplesAndInputs2)
   MakeUniquePeakCalls <- MakeUniquePeakCalls[match(unique(MakeUniquePeakCalls[,1]),MakeUniquePeakCalls[,1]),]   
   SamplesAndInputs <- matrix(MakeUniquePeakCalls,ncol=3,byrow=F)

   SamplesAndInputs[SamplesAndInputs[,3] %in% "Too_few_Reads_To_Calculate" | SamplesAndInputs[,3] %in% "No_Information_Available" | is.na(SamplesAndInputs[,3]),3] <-  ShiftSizeDefault

  Variables  <- c(file.path(PLs@TPICsDir,""),file.path(PLs@BamDir,""),WkgDir,GenomeForTPICS,min_size,WideScale)
  names(Variables) <- c("TPICsDirectory","BamDirectory","WorkingDirectory","GT","min_size","WideScale")
  

  if(nrow(SamplesAndInputs) > 0){
    Specialisations  <- vector("list",length=nrow(SamplesAndInputs))
    for(i in 1:length(Specialisations)){
      Specialisations[[i]] <- c(as.vector(SamplesAndInputs[i,1]),as.vector(SamplesAndInputs[i,2]),as.vector(SamplesAndInputs[i,3]))
      names(Specialisations[[i]]) <- c("Test","Control","SS")  
    } 
    names(Specialisations) <-  paste(as.vector(SamplesAndInputs[,1]),"_TPICsPeakCall",sep="") 
  
    Pipeline = "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/TPICSPeakCallingPipeline.xml"  
    PipeName <- "SS_TPICsPeak"
    SSTPICsPeak_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
    saveXML(SSTPICsPeak_WfMeta,file=file.path(PLs@WorkFlowDir,"SS_TPICsPeak.xml"))
    system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"SS_TPICsPeak.xml"),sep=""),wait=T)
  }


  SampleSheet <- ReadAndLock(file.path(WkgDir,"SampleSheet.csv"),WkdDir,SAF=F,napTime=5)

  TPICsPeaksFull <-  dir(path=PLs@TPICsDir,recursive=T,full.names=T)
    
  for(i in 1:nrow(SampleSheet)){
	 if(any(file.path(PLs@TPICsDir,gsub(".bam","",SampleSheet[i,"Processed_bamFileName"]),paste(gsub(".bam","",SampleSheet[i,"Processed_bamFileName"]),"_TPICS_Peaks.bed",sep="")) %in% TPICsPeaksFull)){
	   	TPICsToRead <- TPICsPeaksFull[TPICsPeaksFull %in% file.path(PLs@TPICsDir,gsub(".bam","",SampleSheet[i,"Processed_bamFileName"]),paste(gsub(".bam","",SampleSheet[i,"Processed_bamFileName"]),"_TPICS_Peaks.bed",sep=""))]
 	    print(TPICsToRead)
 	    if(file.info(TPICsToRead)$size > 0){
		    DataIn <- read.delim(TPICsToRead,sep="\t",comment.char="#")
		    SampleSheet[i,"TPICsPeaks"] <- nrow(DataIn)		
		    SampleSheet[i,"TPICs_name"] <- TPICsToRead
		  }else{
		    SampleSheet[i,"TPICsPeaks"] <- 0		
		    SampleSheet[i,"TPICs_name"] <- TPICsToRead		  		  
		  }
		  
		  print(nrow(DataIn))
    }
  }
        WriteAndUnlock(SampleSheet,file.path(WkgDir,"SampleSheet.csv"))	
        return(SampleSheet)
}
###########################


RunPeakProfilingPipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipelineLocations,Config="Config",Caller="Macs"){
   
  if(Caller == "Macs"){
  PeakDirectory <- PLs@MacsDir
  PeakFileNameColumn <- colnames(SampleSheet) %in% "Macs_name"
  } 
  if(Caller == "Sicer"){
  PeakDirectory <- PLs@SicerDir
  PeakFileNameColumn <- colnames(SampleSheet) %in% "Sicer_name"  
  } 
  if(Caller == "TPICs"){
  PeakDirectory <- PLs@TPICsDir
  PeakFileNameColumn <- colnames(SampleSheet) %in% "TPICs_name"  
  }   
  BamDir <- PLs@BamDir
  
  genome <- GetGenomeFromConfig(WkgDir,ConfigDirectory="Config") 
  genomeChrLengths <- GetChrLengthsFromConfig(WkgDir,ConfigDirectory="Config")
  fastaFromGenome <- GetGenomeBuildFromConfig(WkgDir,ConfigDirectory="Config")
  Variables  <- c(WkgDir,file.path(PeakDirectory,""),file.path(BamDir,""),genome,genomeChrLengths,fastaFromGenome)
  names(Variables) <- c("WorkingDirectory","PeakDirectory","BamDirectory","genome","genomeFile","fastafile")
  
  JustOfInterest <- SampleSheet[!is.na(SampleSheet[,PeakFileNameColumn]) & !SampleSheet[,PeakFileNameColumn] %in% "NA",]

  dir.create(file.path(PeakDirectory,"PeakProfiles"),recursive=T,showWarnings=F)
  if(nrow(JustOfInterest) > 0){
    Specialisations  <- vector("list",length=nrow(JustOfInterest))
    for(i in 1:length(Specialisations)){
      Specialisations[[i]] <- c(gsub(".xls",".bed",as.vector(JustOfInterest[i,PeakFileNameColumn])),as.vector(JustOfInterest[i,"GenomicsID"]),as.vector(JustOfInterest[i,"Processed_bamFileName"]),36,as.vector(JustOfInterest[i,"GenomicsID"]))
      names(Specialisations[[i]]) <- c("peakbed","mergedpeakbed","bam","ReadLength","Test")  
    } 
    names(Specialisations) <-  paste(as.vector(JustOfInterest[,1]),paste("_",Caller,"PeakCall",sep=""),sep="") 
  
    Pipeline = "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/PeakProfilingPipeline2.xml"  
    PipeName <- paste("SS_",Caller,"PeakProfile",sep="")
    SSPeakProfile_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
    saveXML(SSPeakProfile_WfMeta,file=file.path(PLs@WorkFlowDir,paste("SS_",Caller,"PeakProfile.xml",sep="")))
    system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,paste("SS_",Caller,"PeakProfile.xml",sep="")),sep=""),wait=T)
  }
if(Caller %in% "Macs"){
      PeakGenes <- dir(path=file.path(PeakDirectory,"PeakProfiles"),pattern="*Annotated.xls$")
    for(i in 1:nrow(SampleSheet)){
    	if(gsub("_Processed.bam","",SampleSheet[i,"Processed_bamFileName"]) %in% gsub("_Processed_peaks_Annotated.xls","",PeakGenes)){
    		MacsToAnnotate <- file.path(WkgDir,"Peaks","Macs_Peaks",PeakGenes [gsub("_Processed_peaks_Annotated.xls","",PeakGenes) %in% gsub("_Processed.bam","",SampleSheet[i,"Processed_bamFileName"])])
    		SampleSheet[i,paste(Caller,"_Genes_Annotation",sep="")] <- MacsToAnnotate
    	}
    }
    
        PeakGenes <- dir(path=file.path(PeakDirectory,"PeakProfiles"),pattern="*Annotated.summary$")
    for(i in 1:nrow(SampleSheet)){
    	if(gsub("_Processed.bam","",SampleSheet[i,"Processed_bamFileName"]) %in% gsub("_Processed_peaks_Annotated.summary","",PeakGenes)){
    		MacsToAnnotateSum <- file.path(PeakDirectory,"",PeakGenes [gsub("_Processed_peaks_Annotated.summary","",PeakGenes) %in% gsub("_Processed.bam","",SampleSheet[i,"Processed_bamFileName"])])
    		DataInAnno <- read.delim(MacsToAnnotateSum,sep="\t",comment.char="#",header=T)[,1]
    		SampleSheet[i,paste(Caller,"_Genes_In_Peaks",sep="")] <- DataInAnno[1]
    		SampleSheet[i,paste(Caller,"_Peaks_In_Genes",sep="")] <- DataInAnno[2]
    	}
    }
    PeaksGO <- dir(path=file.path(PeakDirectory,"PeakProfiles"),pattern="*_GO_Results.txt$")
    for(i in 1:nrow(SampleSheet)){
    	if(gsub("_Processed.bam","",SampleSheet[i,"Processed_bamFileName"]) %in% gsub("_Processed_GO_Results.txt","",PeaksGO)){
    		PeaksGOToAnnotateSum <- file.path(PeakDirectory,"",PeaksGO [gsub("_Processed_GO_Results.txt","",PeaksGO) %in% gsub("_Processed.bam","",SampleSheet[i,"Processed_bamFileName"])])
    		DataInAnno <- read.delim(PeaksGOToAnnotateSum,sep="\t",comment.char="#",header=T)
    		SampleSheet[i,paste(Caller,"_GO_Terms",sep="")] <- nrow(DataInAnno[DataInAnno[,"Adjusted_P_Value"] < 0.01 & !is.na(DataInAnno[,"Adjusted_P_Value"]),])
    		SampleSheet[i,paste(Caller,"_GO_Table",sep="")] <- PeaksGOToAnnotateSum
    	}
    
    }
}



}

RunPeakMotifingPipeline <- function(SampleSheet,WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipelineLocations,Config="Config",Caller="Macs",NPeaks=500){
   
  if(Caller == "Macs"){
  PeakDirectory <- PLs@MacsDir
  PeakFileNameColumn <- colnames(SampleSheet) %in% "Macs_name"
  ColumnIndexForRank <- 5
  RankOrder <- "Rank"
  } 
  if(Caller == "Sicer"){
  PeakDirectory <- PLs@SicerDir
  PeakFileNameColumn <- colnames(SampleSheet) %in% "Sicer_name" 
  ColumnIndexForRank <- 6  
  RankOrder <- "RevRank"
  } 
  if(Caller == "TPICs"){
  PeakDirectory <- PLs@TPICsDir
  PeakFileNameColumn <- colnames(SampleSheet) %in% "TPICs_name"  
  ColumnIndexForRank <- 5
  RankOrder <- "RevRank"    
  }   
  BamDir <- PLs@BamDir
  MemeFormatdb <- "/lustre/mib-cri/carrol09/Work/MyPipe/NewMeme/TranfacMatrix.meme"
  genome <- GetGenomeFromConfig(WkgDir,ConfigDirectory="Config") 
  genomeChrLengths <- GetChrLengthsFromConfig(WkgDir,ConfigDirectory="Config")
  fastaFromGenome <- GetGenomeBuildFromConfig(WkgDir,ConfigDirectory="Config")
  Variables  <- c(WkgDir,file.path(PeakDirectory,""),file.path(BamDir,""),genome,genomeChrLengths,fastaFromGenome,NPeaks,RankOrder,ColumnIndexForRank,MemeFormatdb)
  names(Variables) <- c("WorkingDirectory","PeakDirectory","BamDirectory","genome","genomeFile","Fasta","NPeaks","RankOrder","RankColumn","MotifDatabaseLocation")
  
  JustOfInterest <- SampleSheet[!is.na(SampleSheet[,PeakFileNameColumn]) & !SampleSheet[,PeakFileNameColumn] %in% "NA",]
if(nrow(JustOfInterest) > 0){
  #  dir.create(file.path(PeakDirectory,"PeakProfiles"),recursive=T)
  if(Caller == "Macs"){  
    Specialisations  <- vector("list",length=nrow(JustOfInterest))
    for(i in 1:length(Specialisations)){
      Specialisations[[i]] <- c("",gsub(".xls",".bed",basename(as.vector(JustOfInterest[i,PeakFileNameColumn]))),as.vector(JustOfInterest[i,"GenomicsID"]))
      names(Specialisations[[i]]) <- c("SamplePeakDir","PeakFileName","Test")  
    } 
    names(Specialisations) <-  paste(as.vector(JustOfInterest[,1]),paste("_",Caller,"PeakCall",sep=""),sep="") 
  }
  if(Caller %in% c("Sicer","TPICs")){  
    Specialisations  <- vector("list",length=nrow(JustOfInterest))
    for(i in 1:length(Specialisations)){
      Specialisations[[i]] <- c(gsub(".bam","",as.vector(JustOfInterest[i,"Processed_bamFileName"])),gsub(".xls",".bed",basename(as.vector(JustOfInterest[i,PeakFileNameColumn]))),as.vector(JustOfInterest[i,"GenomicsID"]))
      names(Specialisations[[i]]) <- c("SamplePeakDir","PeakFileName","Test")  
    } 
    names(Specialisations) <-  paste(as.vector(JustOfInterest[,1]),paste("_",Caller,"PeakCall",sep=""),sep="") 
  }
  
  
    Pipeline = "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/MotifProcessPipeline2.xml"  
    PipeName <- paste("SS_",Caller,"Motifs",sep="")
    SSPeakProfile_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
    saveXML(SSPeakProfile_WfMeta,file=file.path(PLs@WorkFlowDir,paste("SS_",Caller,"MotifProcess.xml",sep="")))
    system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,paste("SS_",Caller,"MotifProcess.xml",sep="")),sep=""),wait=T)
}
  
  SampleSheet <- ReadAndLock(file.path(WkgDir,"SampleSheet.csv"),WkgDir,SAF=F,napTime=5)

  for(i in 1:nrow(SampleSheet)){
  	 SampleToLookFor <- SampleSheet[i,"GenomicsID"]
  	 KnownMotifFile <- dir(path=file.path(PeakDirectory,"Motif",SampleToLookFor,"Known"),pattern="*me.txt$",full.names=T)
  	 DataIn <- 0
    	 if(all(c(length(KnownMotifFile) > 0,file.info(KnownMotifFile)$size > 0))){
    		SampleSheet[i,"Known_Motif_File"] <- KnownMotifFile
    		print(KnownMotifFile)
    		try(
    		DataIn <- read.delim(KnownMotifFile,sep=" ",comment.char="#",skip=11,header=F)
    		,silent=T)
    		if(DataIn != 0 & !is.null(DataIn)){
      		try(
      		SampleSheet[i,"Known_Significant_Motifs"]  <- nrow(DataIn)
      		,silent=T)
    		}
        		
      }
      DataIn <- 0
    }

    for(i in 1:nrow(SampleSheet)){
    	SampleToLookFor <- as.vector(SampleSheet[i,"GenomicsID"])
    	print(SampleToLookFor)
    	DremeMotifFile <- dir(path=file.path(PeakDirectory,"Motif",SampleToLookFor,"Denovo","dreme_out"),pattern="*.xml$",full.names=T)
    	if(all(c(length(DremeMotifFile) > 0,file.info(DremeMotifFile)$size > 0))){
    		doc = xmlTreeParse(DremeMotifFile, useInternal = TRUE)
    		top = xmlRoot(doc)
    		NOfMotifs <- length(names(top[["motifs"]]))
    		SampleSheet[i,"Dreme_Motif_File"] <- dir(path=file.path(PeakDirectory,"Motif",SampleToLookFor,"Denovo","dreme_out"),pattern="*.html$",full.names=T)
    		SampleSheet[i,"Dreme_Significant_Motifs"] <- NOfMotifs
    	}
    }


      for(i in 1:nrow(SampleSheet)){
    	
      SampleToLookFor <- as.vector(SampleSheet[i,"GenomicsID"])    	
    	DremeTomTomFile <- dir(path=file.path(PeakDirectory,"Motif",SampleToLookFor,"Denovo","dreme_tomtom_out"),pattern="*.txt$",full.names=T)
    	
      if(all(c(length(DremeTomTomFile) > 0,file.info(DremeTomTomFile)$size > 0))){
    		TempTomTom <- read.delim(DremeTomTomFile,sep="\t",header=T)
    		if(nrow(TempTomTom) > 0){
    		PerSampleTomTom <- cbind(SampleToLookFor,TempTomTom)
    		if(exists(as.character(bquote(SignificantTomTomDreme)))){
    			SignificantTomTomDreme <- rbind(SignificantTomTomDreme,PerSampleTomTom)
    		}else{
    			SignificantTomTomDreme <- PerSampleTomTom 
    		}
    			SampleSheet[i,"Dreme_Significant_Motifs_Matched_To_Known"] <- length(unique(PerSampleTomTom[,2]))
    		}else{
    			SampleSheet[i,"Dreme_Significant_Motifs_Matched_To_Known"] <- 0
    		}
    	}
}

      for(i in 1:nrow(SampleSheet)){
      	SampleToLookFor <- SampleSheet[i,"GenomicsID"]
      	MemeMotifFile <- dir(path=file.path(PeakDirectory,"Motif",SampleToLookFor,"Denovo","meme_out"),pattern="*.xml$",full.names=T)
      	if(all(c(length(MemeMotifFile) > 0,file.info(MemeMotifFile)$size > 0))){
      		doc = xmlTreeParse(MemeMotifFile, useInternal = TRUE)
      		top = xmlRoot(doc)
      		NOfMotifs <- length(names(top[["motifs"]]))
      		SampleSheet[i,"meme_Motif_File"] <- dir(path=file.path(PeakDirectory,"Motif",SampleToLookFor,"Denovo","meme_out"),pattern="*.html$",full.names=T)
      		SampleSheet[i,"meme_Significant_Motifs"] <- NOfMotifs	
        	}
        }

      for(i in 1:nrow(SampleSheet)){
      print(i)
      	SampleToLookFor <- SampleSheet[i,"GenomicsID"]
       	memeTomTomFile <- dir(path=file.path(PeakDirectory,"Motif",SampleToLookFor,"Denovo","meme_tomtom_out"),pattern="*.txt$",full.names=T)
      	if(all(c(length(memeTomTomFile) > 0,file.info(memeTomTomFile)$size > 0))){
      		TempTomTom <- read.delim(memeTomTomFile,sep="\t",header=T)
      		if(nrow(TempTomTom) > 0){
      		PerSampleTomTom <- cbind(SampleToLookFor,TempTomTom)
      		if(exists(as.character(bquote(SignificantTomTommeme)))){
      			SignificantTomTommeme <- rbind(SignificantTomTommeme,PerSampleTomTom)
      		}else{
      			SignificantTomTommeme <- PerSampleTomTom 
      		}
      		SampleSheet[i,"meme_Significant_Motifs_Matched_To_Known"] <- length(unique(PerSampleTomTom[,2]))
      		}else{
      		SampleSheet[i,"meme_Significant_Motifs_Matched_To_Known"] <- 0
      		}
    	 }
      }

      WriteAndUnlock(SampleSheet,file.path(WkgDir,"SampleSheet.csv"))	

        
    	
    	
    	
}


TopPeaksByRank <- function(InFile,OutFile,RankColumn,RankOrder,NPeaks){
  TempPeaks <-   read.delim(InFile,sep="\t",header=F)
  if(RankOrder %in% "RevRank"){
     NoOfPeaks <- nrow(TempPeaks)
     if(NoOfPeaks < NPeaks){NPeaks <- NoOfPeaks}
     ActualPeaks <- TempPeaks[order(TempPeaks[,RankColumn],decreasing=F),][1:NPeaks,]  
  }
  if(RankOrder %in% "Rank"){
     NoOfPeaks <- nrow(TempPeaks)
     if(NoOfPeaks < NPeaks){NPeaks <- NoOfPeaks}
     ActualPeaks <- TempPeaks[order(TempPeaks[,RankColumn],decreasing=T),][1:NPeaks,]          
  }
  write.table(ActualPeaks,OutFile,sep="\t",row.names=F,col.names=F,quote=F)              
}

RunMainPipeline <- function(WkgDir=WkgDir,JobString,MaxJobs=75,PLs=PipelineLocations,Config="Config"){
   
#  genome <- GetGenomeFromConfig(WkgDir,ConfigDirectory=Config)

  WorkFlowDirectory <- PLs@WorkFlowDir
  Config2 <- file.path(WkgDir,Config,"config.ini")
  R_executable <- "/lustre/mib-cri/carrol09/Work/MyPipe/R/R-2.15.0/bin/Rscript"
  RandomTrackerNumber <-  getRandString()
  Variables  <- c(WorkFlowDirectory,RandomTrackerNumber,Config2,R_executable)
  names(Variables) <- c("WorkflowDir","PathwayTracker","Config","R_Executable")
  Specialisations <- vector("list")
  #Specialisations[[1]] <- "/lustre/mib-cri/carrol09/Work/PipelinePracticeSet/20121114_MontoyaAR_DN_Hes6ChIP/bamFiles/ID-LNCAP-LM-HES6-BICALUTAMIDE-INPUT-D26.bwa.bam"

  Pipeline = "/lustre/mib-cri/carrol09/Work/MyPipe/Process10/src/main/pipelines/MainPipeline2.xml"  
  PipeName <- "Main"
  Main_WfMeta <- MakeWorkFlowXML(JobString,Variables,Specialisations,Pipeline,PipeName,WkgDir,Config,maxJobs=75)
  saveXML(Main_WfMeta,file=file.path(PLs@WorkFlowDir,"Main.xml"))
  system(paste("java -jar /lustre/mib-cri/carrol09/MyPipe/workflow-all-1.2-SNAPSHOT.jar --mode=lsf ",file.path(PLs@WorkFlowDir,"Main.xml"),sep=""),wait=T)

}

